[PATCH 1/7] Rename 'hello2.spec.' -> 'hello2.spec' 'hello3.spec.' -> 'hello3.spec'

2022-09-20 Thread Yonggang Luo via Elfutils-devel
These filenames are invalid on win32

Signed-off-by: Yonggang Luo 
---
 tests/Makefile.am   | 2 +-
 tests/debuginfod-rpms/{hello2.spec. => hello2.spec} | 0
 tests/debuginfod-rpms/{hello3.spec. => hello3.spec} | 0
 3 files changed, 1 insertion(+), 1 deletion(-)
 rename tests/debuginfod-rpms/{hello2.spec. => hello2.spec} (100%)
 rename tests/debuginfod-rpms/{hello3.spec. => hello3.spec} (100%)

diff --git a/tests/Makefile.am b/tests/Makefile.am
index 85514898..fc2235f4 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -563,7 +563,7 @@ EXTRA_DIST = run-arextract.sh run-arsymtest.sh run-ar.sh \
 debuginfod-rpms/fedora31/hello3-debugsource-1.0-2.x86_64.rpm \
 debuginfod-rpms/fedora31/hello3-two-1.0-2.x86_64.rpm \
 debuginfod-rpms/fedora31/hello3-two-debuginfo-1.0-2.x86_64.rpm \
-debuginfod-rpms/hello2.spec. \
+debuginfod-rpms/hello2.spec \
 debuginfod-rpms/rhel6/hello2-1.0-2.i686.rpm \
 debuginfod-rpms/rhel6/hello2-1.0-2.src.rpm \
 debuginfod-rpms/rhel6/hello2-debuginfo-1.0-2.i686.rpm \
diff --git a/tests/debuginfod-rpms/hello2.spec. 
b/tests/debuginfod-rpms/hello2.spec
similarity index 100%
rename from tests/debuginfod-rpms/hello2.spec.
rename to tests/debuginfod-rpms/hello2.spec
diff --git a/tests/debuginfod-rpms/hello3.spec. 
b/tests/debuginfod-rpms/hello3.spec
similarity index 100%
rename from tests/debuginfod-rpms/hello3.spec.
rename to tests/debuginfod-rpms/hello3.spec
-- 
2.36.1.windows.1



[PATCH 3/7] lib: Use NOT_HAVE_LIBINTL to guard if need #include

2022-09-20 Thread Yonggang Luo via Elfutils-devel
Signed-off-by: Yonggang Luo 
---
 lib/eu-config.h | 8 
 1 file changed, 8 insertions(+)

diff --git a/lib/eu-config.h b/lib/eu-config.h
index c7d7cbb2..5e6c3c72 100644
--- a/lib/eu-config.h
+++ b/lib/eu-config.h
@@ -52,9 +52,17 @@
 # define rwlock_unlock(lock) ((void) (lock))
 #endif /* USE_LOCKS */
 
+#if !defined(NOT_HAVE_LIBINTL)
+#include 
+#endif
+
 /* gettext helper macros.  */
 #define N_(Str) Str
+#if !defined(NOT_HAVE_LIBINTL)
 #define _(Str) dgettext ("elfutils", Str)
+#else
+#define _(Str) N_(Str)
+#endif
 
 /* Compiler-specific definitions.  */
 #define strong_alias(name, aliasname) \
-- 
2.36.1.windows.1



[PATCH 0/7] Enable building libelf of elfutils on win32

2022-09-20 Thread Yonggang Luo via Elfutils-devel
After these changes, now libelf can be build with mingw/gcc and msvc/clang

Yonggang Luo (7):
  Rename 'hello2.spec.' -> 'hello2.spec' 'hello3.spec.' -> 'hello3.spec'
  move platform depended include into system.h
  lib: Use NOT_HAVE_LIBINTL to guard if need #include 
  Remove duplicated #include 
  Strip __ prefix from __BYTE_ORDER __LITTLE_ENDIAN and __BIG_ENDIAN
  Fixes building with msvc/clang mingw/gcc
  Add CMake build files

 .gitignore|   1 +
 CMakeLists.txt|  47 +
 configure.ac  |   1 +
 debuginfod/debuginfod.cxx |   1 -
 lib/color.c   |   1 -
 lib/eu-config.h   |   8 +
 lib/printversion.c|   1 -
 lib/system.h  |  74 ++-
 lib/system_win32.c| 162 +++
 lib/xasprintf.c   |   1 -
 lib/xmalloc.c |   1 -
 libasm/asm_end.c  |   1 -
 libasm/asm_error.c|   1 -
 libasm/asm_newscn.c   |   1 -
 libcpu/i386_disasm.c  |   2 +-
 libcpu/i386_lex.l |   1 -
 libcpu/i386_parse.y   |   1 -
 libcpu/memory-access.h|  26 +--
 libcpu/riscv_disasm.c |   2 +-
 libdw/libdwP.h|   1 -
 libdw/memory-access.h |   8 +-
 libdwfl/argp-std.c|   1 -
 libdwfl/dwfl_error.c  |   1 -
 libdwfl/dwfl_segment_report_module.c  |   2 +-
 libebl/eblmachineflagname.c   |   1 -
 libebl/eblobjnotetypename.c   |   2 -
 libebl/eblopenbackend.c   |   1 -
 libebl/libeblP.h  |   1 -
 libelf/CMakeLists.txt | 192 ++
 libelf/common.h   |   5 +-
 libelf/config-cmake/config.h  |  27 +++
 libelf/elf32_checksum.c   |   5 +-
 libelf/elf32_getphdr.c|   2 -
 libelf/elf32_getshdr.c|   2 -
 libelf/elf32_updatefile.c |   6 +-
 libelf/elf32_updatenull.c |   2 -
 libelf/elf32_xlatetof.c   |   5 +-
 libelf/elf32_xlatetom.c   |   1 -
 libelf/elf_begin.c|  10 +-
 libelf/elf_cntl.c |   2 -
 libelf/elf_compress.c |   2 -
 libelf/elf_end.c  |   3 +-
 libelf/elf_error.c|   1 -
 libelf/elf_getarsym.c |  10 +-
 libelf/elf_getdata.c  |   2 -
 libelf/elf_getdata_rawchunk.c |   2 -
 libelf/elf_getshdrstrndx.c|   2 -
 libelf/elf_readall.c  |   2 -
 libelf/elf_update.c   |   7 +-
 libelf/gelf_xlate.c   |   1 -
 libelf/libelf.h   |   6 +
 libelf/libelfP.h  |   5 +
 libelf/nlist.c|   1 -
 lib/xasprintf.c => libelf/win32/ar.h  | 104 +-
 src/addr2line.c   |   1 -
 src/ar.c  |   1 -
 src/arlib-argp.c  |   1 -
 src/arlib.c   |   1 -
 src/arlib.h   |   2 +-
 src/arlib2.c  |   1 -
 src/elfcmp.c  |   1 -
 src/elflint.c |   1 -
 src/findtextrel.c |   1 -
 src/nm.c  |   1 -
 src/objdump.c |   1 -
 src/ranlib.c  |   1 -
 src/readelf.c |   1 -
 src/size.c|   1 -
 src/strings.c |   1 -
 src/strip.c   |   1 -
 src/unstrip.c |   1 -
 tests/Makefile.am |   2 +-
 .../{hello2.spec. => hello2.spec} |   0
 .../{hello3.spec. => hello3.spec} |   0
 74 files changed, 612 insertions(+), 166 deletions(-)
 create mode 100644 CMakeLists.txt
 create mode 100644 lib/system_win32.c
 create mode 100644 libelf/CMakeLists.txt
 create mode 100644 libelf/config-cmake/config.h
 copy lib/xasprintf.c => libelf/win32/ar.h (66%)
 rename tests/debuginfod-rpms/{hello2.spec. => hello2.spec} (100%)
 rename tests/debuginfod-rpms/{hello3.spec. => hello3.spec} (100%)

-- 
2.36.1.windows.1



[PATCH 6/7] Fixes building with msvc/clang mingw/gcc

2022-09-20 Thread Yonggang Luo via Elfutils-devel
Signed-off-by: Yonggang Luo 
---
 configure.ac  |   1 +
 lib/system.h  |  61 +-
 lib/system_win32.c| 162 ++
 libelf/elf32_updatefile.c |   3 +-
 libelf/elf_begin.c|   7 +-
 libelf/elf_end.c  |   2 +
 libelf/elf_update.c   |   5 +-
 libelf/libelf.h   |   6 ++
 libelf/libelfP.h  |   4 +
 libelf/win32/ar.h |  52 
 10 files changed, 298 insertions(+), 5 deletions(-)
 create mode 100644 lib/system_win32.c
 create mode 100644 libelf/win32/ar.h

diff --git a/configure.ac b/configure.ac
index 03b67a9d..63aeb748 100644
--- a/configure.ac
+++ b/configure.ac
@@ -428,6 +428,7 @@ AC_CHECK_DECLS([memrchr, rawmemchr],[],[],
[#define _GNU_SOURCE
 #include ])
 AC_CHECK_DECLS([powerof2],[],[],[#include ])
+AC_CHECK_DECLS([mmap],[],[],[#include ])
 AC_CHECK_DECLS([mempcpy],[],[],
[#define _GNU_SOURCE
 #include ])
diff --git a/lib/system.h b/lib/system.h
index bbbe06c4..05b5cf8d 100644
--- a/lib/system.h
+++ b/lib/system.h
@@ -40,21 +40,78 @@
 #include 
 
 /* System dependend headers */
+#if !defined(_WIN32)
 #include 
 #include 
-#include 
+#endif
+
+#if defined(_MSC_VER)
+#include 
+#include 
+#include 
+#include 
+#include 
+typedef SSIZE_T ssize_t;
+#else
 #include 
 #include 
+#endif
+
+#if HAVE_DECL_MMAP
+#include 
+#endif
 
 #if defined(HAVE_ERROR_H)
 #include 
-#elif defined(HAVE_ERR_H)
+#elif defined(HAVE_ERR_H) || defined(_MSC_VER)
 extern int error_message_count;
 void error(int status, int errnum, const char *format, ...);
 #else
 #error "err.h or error.h must be available"
 #endif
 
+#if defined(_MSC_VER)
+#define BIG_ENDIAN 4321
+#define LITTLE_ENDIAN 1234
+#define BYTE_ORDER LITTLE_ENDIAN
+static inline int ftruncate(int fd, off_t length)
+{
+ return _chsize_s(fd, length);
+}
+#endif /* defined(_MSC_VER) */
+
+#if defined(_WIN32)
+static inline uint16_t bswap_16(uint16_t x) {
+  return x) >> 8) & 0xff) | (((x) & 0xff) << 8));
+}
+
+static inline uint32_t bswap_32(uint32_t x) {
+  return (
+(((x) & 0xff00) >> 24) | (((x) & 0x00ff) >>  8) |
+(((x) & 0xff00) <<  8) | (((x) & 0x00ff) << 24)
+  );
+}
+
+static inline uint64_t bswap_64(uint64_t x) {
+  return (
+(((x) & 0xff00ull) >> 56) |
+(((x) & 0x00ffull) >> 40) |
+(((x) & 0xff00ull) >> 24) |
+(((x) & 0x00ffull) >> 8)  |
+(((x) & 0xff00ull) << 8)  |
+(((x) & 0x00ffull) << 24) |
+(((x) & 0xff00ull) << 40) |
+(((x) & 0x00ffull) << 56)
+  );
+}
+
+#define htobe64(x) bswap_64(x)
+#define be64toh(x) bswap_64(x)
+
+ssize_t pread(int fd, void *buf, size_t count, off_t offset);
+ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset);
+#endif /* defined(_WIN32) */
+
 /* error (EXIT_FAILURE, ...) should be noreturn but on some systems it
isn't.  This may cause warnings about code that should not be reachable.
So have an explicit error_exit wrapper that is noreturn (because it
diff --git a/lib/system_win32.c b/lib/system_win32.c
new file mode 100644
index ..d9941cc5
--- /dev/null
+++ b/lib/system_win32.c
@@ -0,0 +1,162 @@
+/* Copyright (C) 2022 Yonggang Luo
+   This file is part of elfutils.
+
+   This file is free software; you can redistribute it and/or modify
+   it under the terms of either
+
+ * the GNU Lesser General Public License as published by the Free
+   Software Foundation; either version 3 of the License, or (at
+   your option) any later version
+
+   or
+
+ * the GNU General Public License as published by the Free
+   Software Foundation; either version 2 of the License, or (at
+   your option) any later version
+
+   or both in parallel, as here.
+
+   elfutils is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   General Public License for more details.
+
+   You should have received copies of the GNU General Public License and
+   the GNU Lesser General Public License along with this program.  If
+   not, see .  */
+
+#include 
+#include "system.h"
+
+#include 
+
+static const struct
+{
+  DWORD winerr;
+  int doserr;
+} doserrors[] =
+
+{
+{ERROR_INVALID_FUNCTION, EINVAL},
+{ERROR_FILE_NOT_FOUND, ENOENT},
+{ERROR_PATH_NOT_FOUND, ENOENT},
+{ERROR_TOO_MANY_OPEN_FILES, EMFILE},
+{ERROR_ACCESS_DENIED, EACCES},
+{ERROR_INVALID_HANDLE, EBADF},
+{ERROR_ARENA_TRASHED, ENOMEM},
+{ERROR_NOT_ENOUGH_MEMORY, ENOMEM},
+{ERROR_INVALID_BLOCK, ENOMEM},
+{ERROR_BAD_ENVIRONMENT, E2BIG},
+{ERROR_BAD_FORMAT, ENOEXEC},
+{ERROR_INVALID_ACCESS, EINVAL},
+{ERROR_INVALID_DATA, EINVAL},
+{ERROR_IN

[PATCH 4/7] Remove duplicated #include

2022-09-20 Thread Yonggang Luo via Elfutils-devel
Signed-off-by: Yonggang Luo 
---
 debuginfod/debuginfod.cxx | 1 -
 lib/color.c   | 1 -
 lib/printversion.c| 1 -
 lib/xasprintf.c   | 1 -
 lib/xmalloc.c | 1 -
 libasm/asm_end.c  | 1 -
 libasm/asm_error.c| 1 -
 libasm/asm_newscn.c   | 1 -
 libcpu/i386_lex.l | 1 -
 libcpu/i386_parse.y   | 1 -
 libdw/libdwP.h| 1 -
 libdwfl/argp-std.c| 1 -
 libdwfl/dwfl_error.c  | 1 -
 libebl/libeblP.h  | 1 -
 src/addr2line.c   | 1 -
 src/ar.c  | 1 -
 src/arlib-argp.c  | 1 -
 src/arlib.c   | 1 -
 src/arlib2.c  | 1 -
 src/elfcmp.c  | 1 -
 src/elflint.c | 1 -
 src/findtextrel.c | 1 -
 src/nm.c  | 1 -
 src/objdump.c | 1 -
 src/ranlib.c  | 1 -
 src/readelf.c | 1 -
 src/size.c| 1 -
 src/strings.c | 1 -
 src/strip.c   | 1 -
 src/unstrip.c | 1 -
 30 files changed, 30 deletions(-)

diff --git a/debuginfod/debuginfod.cxx b/debuginfod/debuginfod.cxx
index 000a41c4..8e7ee499 100644
--- a/debuginfod/debuginfod.cxx
+++ b/debuginfod/debuginfod.cxx
@@ -47,7 +47,6 @@ extern "C" {
 
 #include 
 #include 
-#include 
 #include 
 #include 
 #include 
diff --git a/lib/color.c b/lib/color.c
index e43b6143..bd105ec2 100644
--- a/lib/color.c
+++ b/lib/color.c
@@ -32,7 +32,6 @@
 #endif
 
 #include 
-#include 
 #include 
 #include 
 #include 
diff --git a/lib/printversion.c b/lib/printversion.c
index f657329c..7d31fa83 100644
--- a/lib/printversion.c
+++ b/lib/printversion.c
@@ -30,7 +30,6 @@
 # include 
 #endif
 
-#include 
 #include "printversion.h"
 
 void
diff --git a/lib/xasprintf.c b/lib/xasprintf.c
index 179ea2e8..d61fad90 100644
--- a/lib/xasprintf.c
+++ b/lib/xasprintf.c
@@ -33,7 +33,6 @@
 #include 
 #include 
 #include 
-#include 
 #include "libeu.h"
 #include "system.h"
 
diff --git a/lib/xmalloc.c b/lib/xmalloc.c
index 7c094985..de7211f1 100644
--- a/lib/xmalloc.c
+++ b/lib/xmalloc.c
@@ -30,7 +30,6 @@
 # include 
 #endif
 
-#include 
 #include 
 #include 
 #include 
diff --git a/libasm/asm_end.c b/libasm/asm_end.c
index 077d2aa5..a26120f2 100644
--- a/libasm/asm_end.c
+++ b/libasm/asm_end.c
@@ -32,7 +32,6 @@
 #endif
 
 #include 
-#include 
 #include 
 #include 
 #include 
diff --git a/libasm/asm_error.c b/libasm/asm_error.c
index cc3e660b..2b769395 100644
--- a/libasm/asm_error.c
+++ b/libasm/asm_error.c
@@ -31,7 +31,6 @@
 # include 
 #endif
 
-#include 
 #include 
 #include 
 
diff --git a/libasm/asm_newscn.c b/libasm/asm_newscn.c
index 1150015f..d258d969 100644
--- a/libasm/asm_newscn.c
+++ b/libasm/asm_newscn.c
@@ -32,7 +32,6 @@
 #endif
 
 #include 
-#include 
 #include 
 #include 
 
diff --git a/libcpu/i386_lex.l b/libcpu/i386_lex.l
index b6ec0f87..9b33ed95 100644
--- a/libcpu/i386_lex.l
+++ b/libcpu/i386_lex.l
@@ -31,7 +31,6 @@
 #endif
 
 #include 
-#include 
 
 #include 
 #include "system.h"
diff --git a/libcpu/i386_parse.y b/libcpu/i386_parse.y
index 9a92c2e0..d2236d59 100644
--- a/libcpu/i386_parse.y
+++ b/libcpu/i386_parse.y
@@ -35,7 +35,6 @@
 #include 
 #include 
 #include 
-#include 
 #include 
 #include 
 #include 
diff --git a/libdw/libdwP.h b/libdw/libdwP.h
index 56b388c1..961fa4e7 100644
--- a/libdw/libdwP.h
+++ b/libdw/libdwP.h
@@ -29,7 +29,6 @@
 #ifndef _LIBDWP_H
 #define _LIBDWP_H 1
 
-#include 
 #include 
 #include 
 
diff --git a/libdwfl/argp-std.c b/libdwfl/argp-std.c
index 01ec18e2..bc013aa0 100644
--- a/libdwfl/argp-std.c
+++ b/libdwfl/argp-std.c
@@ -34,7 +34,6 @@
 #include 
 #include 
 #include 
-#include 
 #include 
 #include 
 
diff --git a/libdwfl/dwfl_error.c b/libdwfl/dwfl_error.c
index a5c683a9..43fa1dcc 100644
--- a/libdwfl/dwfl_error.c
+++ b/libdwfl/dwfl_error.c
@@ -31,7 +31,6 @@
 #endif
 
 #include 
-#include 
 #include 
 #include 
 #include 
diff --git a/libebl/libeblP.h b/libebl/libeblP.h
index fa1c2c9f..c408ed97 100644
--- a/libebl/libeblP.h
+++ b/libebl/libeblP.h
@@ -32,7 +32,6 @@
 #include 
 #include 
 #include 
-#include 
 
 
 /* Backend handle.  */
diff --git a/src/addr2line.c b/src/addr2line.c
index 25db2926..7768b266 100644
--- a/src/addr2line.c
+++ b/src/addr2line.c
@@ -28,7 +28,6 @@
 #include 
 #include 
 #include 
-#include 
 #include 
 #include 
 #include 
diff --git a/src/ar.c b/src/ar.c
index 04456c18..3bcb18fe 100644
--- a/src/ar.c
+++ b/src/ar.c
@@ -24,7 +24,6 @@
 #include 
 #include 
 #include 
-#include 
 #include 
 #include 
 #include 
diff --git a/src/arlib-argp.c b/src/arlib-argp.c
index a3c12e4d..115b9d76 100644
--- a/src/arlib-argp.c
+++ b/src/arlib-argp.c
@@ -20,7 +20,6 @@
 #endif
 
 #include 
-#include 
 
 #include "arlib.h"
 
diff --git a/src/arlib.c b/src/arlib.c
index c09fc3c6..b8f89ba7 100644
--- a/src/arlib.c
+++ b/src/arlib.c
@@ -23,7 +23,6 @@
 #include 
 #include 
 #include 
-#include 
 #include 
 #include 
 #include 
diff --git a/src/arlib2.c b/src/arlib2.c
index 11f44e5d..9ae6662c 100

[PATCH 2/7] move platform depended include into system.h

2022-09-20 Thread Yonggang Luo via Elfutils-devel
All of these files either #include  directly or #include "libelfP.h"
And now "libelfP.h also #include , so the platform depended include
can be moved to system.h safely

Signed-off-by: Yonggang Luo 
---
 lib/system.h  | 11 +++
 libebl/eblmachineflagname.c   |  1 -
 libebl/eblobjnotetypename.c   |  2 --
 libebl/eblopenbackend.c   |  1 -
 libelf/common.h   |  3 ---
 libelf/elf32_checksum.c   |  1 -
 libelf/elf32_getphdr.c|  2 --
 libelf/elf32_getshdr.c|  2 --
 libelf/elf32_updatefile.c |  3 ---
 libelf/elf32_updatenull.c |  2 --
 libelf/elf32_xlatetof.c   |  1 -
 libelf/elf32_xlatetom.c   |  1 -
 libelf/elf_begin.c|  3 ---
 libelf/elf_cntl.c |  2 --
 libelf/elf_compress.c |  2 --
 libelf/elf_end.c  |  1 -
 libelf/elf_error.c|  1 -
 libelf/elf_getarsym.c |  4 
 libelf/elf_getdata.c  |  2 --
 libelf/elf_getdata_rawchunk.c |  2 --
 libelf/elf_getshdrstrndx.c|  2 --
 libelf/elf_readall.c  |  2 --
 libelf/elf_update.c   |  2 --
 libelf/gelf_xlate.c   |  1 -
 libelf/libelfP.h  |  1 +
 libelf/nlist.c|  1 -
 26 files changed, 8 insertions(+), 48 deletions(-)

diff --git a/lib/system.h b/lib/system.h
index d3f42c91..48004df1 100644
--- a/lib/system.h
+++ b/lib/system.h
@@ -35,14 +35,17 @@
 #include 
 #include 
 #include 
-#include 
-#include 
-#include 
-#include 
 #include 
 #include 
 #include 
 
+/* System dependend headers */
+#include 
+#include 
+#include 
+#include 
+#include 
+
 #if defined(HAVE_ERROR_H)
 #include 
 #elif defined(HAVE_ERR_H)
diff --git a/libebl/eblmachineflagname.c b/libebl/eblmachineflagname.c
index 02e11c65..5897735f 100644
--- a/libebl/eblmachineflagname.c
+++ b/libebl/eblmachineflagname.c
@@ -33,7 +33,6 @@
 
 #include 
 #include 
-#include 
 #include 
 
 
diff --git a/libebl/eblobjnotetypename.c b/libebl/eblobjnotetypename.c
index 473a1f2f..2ee1b228 100644
--- a/libebl/eblobjnotetypename.c
+++ b/libebl/eblobjnotetypename.c
@@ -31,8 +31,6 @@
 # include 
 #endif
 
-#include 
-
 #include 
 #include 
 #include 
diff --git a/libebl/eblopenbackend.c b/libebl/eblopenbackend.c
index c6657252..9ba8728b 100644
--- a/libebl/eblopenbackend.c
+++ b/libebl/eblopenbackend.c
@@ -38,7 +38,6 @@
 #include 
 #include 
 
-#include 
 #include 
 
 Ebl *i386_init (Elf *, GElf_Half, Ebl *);
diff --git a/libelf/common.h b/libelf/common.h
index 3718b3fd..4561854f 100644
--- a/libelf/common.h
+++ b/libelf/common.h
@@ -30,9 +30,6 @@
 #ifndef _COMMON_H
 #define _COMMON_H   1
 
-#include 
-#include 
-#include 
 #include 
 #include 
 
diff --git a/libelf/elf32_checksum.c b/libelf/elf32_checksum.c
index c5f27bbe..521668a7 100644
--- a/libelf/elf32_checksum.c
+++ b/libelf/elf32_checksum.c
@@ -32,7 +32,6 @@
 #endif
 
 #include 
-#include 
 #include 
 #include 
 #include 
diff --git a/libelf/elf32_getphdr.c b/libelf/elf32_getphdr.c
index 99b4ac09..4bfca24b 100644
--- a/libelf/elf32_getphdr.c
+++ b/libelf/elf32_getphdr.c
@@ -34,10 +34,8 @@
 #include 
 #include 
 #include 
-#include 
 #include 
 
-#include 
 #include "libelfP.h"
 #include "common.h"
 
diff --git a/libelf/elf32_getshdr.c b/libelf/elf32_getshdr.c
index 237d9122..fc696302 100644
--- a/libelf/elf32_getshdr.c
+++ b/libelf/elf32_getshdr.c
@@ -34,9 +34,7 @@
 #include 
 #include 
 #include 
-#include 
 
-#include 
 #include "libelfP.h"
 #include "common.h"
 
diff --git a/libelf/elf32_updatefile.c b/libelf/elf32_updatefile.c
index 1ff58900..46afa1f4 100644
--- a/libelf/elf32_updatefile.c
+++ b/libelf/elf32_updatefile.c
@@ -37,10 +37,7 @@
 #include 
 #include 
 #include 
-#include 
-#include 
 
-#include 
 #include "libelfP.h"
 
 
diff --git a/libelf/elf32_updatenull.c b/libelf/elf32_updatenull.c
index d0d4d1eb..6c06e5e4 100644
--- a/libelf/elf32_updatenull.c
+++ b/libelf/elf32_updatenull.c
@@ -32,12 +32,10 @@
 #endif
 
 #include 
-#include 
 #include 
 #include 
 #include 
 
-#include 
 #include "libelfP.h"
 #include "elf-knowledge.h"
 
diff --git a/libelf/elf32_xlatetof.c b/libelf/elf32_xlatetof.c
index 082d833f..377659c8 100644
--- a/libelf/elf32_xlatetof.c
+++ b/libelf/elf32_xlatetof.c
@@ -32,7 +32,6 @@
 #endif
 
 #include 
-#include 
 #include 
 
 #include "libelfP.h"
diff --git a/libelf/elf32_xlatetom.c b/libelf/elf32_xlatetom.c
index cb0bb8d5..fa3dc9a7 100644
--- a/libelf/elf32_xlatetom.c
+++ b/libelf/elf32_xlatetom.c
@@ -32,7 +32,6 @@
 #endif
 
 #include 
-#include 
 #include 
 
 #include "libelfP.h"
diff --git a/libelf/elf_begin.c b/libelf/elf_begin.c
index 71eb3594..fe8c640a 100644
--- a/libelf/elf_begin.c
+++ b/libelf/elf_begin.c
@@ -39,11 +39,8 @@
 #include 
 #include 
 #include 
-#include 
-#include 
 #include 
 
-#include 
 #include "libelfP.h"
 #include "common.h"
 
diff --git a/libelf/elf_cntl.c b/libelf/elf_cntl.c
index fd681789..04aa9132 100644
--- a/libelf/elf_cntl.c
+++ b/libelf/elf_cntl.c
@@ -31,8 +31,6 @@
 # include 
 #endif
 
-#include 
-

[PATCH 5/7] Strip __ prefix from __BYTE_ORDER __LITTLE_ENDIAN and __BIG_ENDIAN

2022-09-20 Thread Yonggang Luo via Elfutils-devel
Signed-off-by: Yonggang Luo 
---
 lib/system.h |  4 ++--
 libcpu/i386_disasm.c |  2 +-
 libcpu/memory-access.h   | 26 +-
 libcpu/riscv_disasm.c|  2 +-
 libdw/memory-access.h|  8 
 libdwfl/dwfl_segment_report_module.c |  2 +-
 libelf/common.h  |  2 +-
 libelf/elf32_checksum.c  |  4 ++--
 libelf/elf32_xlatetof.c  |  4 ++--
 libelf/elf_getarsym.c|  6 +++---
 src/arlib.h  |  2 +-
 11 files changed, 31 insertions(+), 31 deletions(-)

diff --git a/lib/system.h b/lib/system.h
index 48004df1..bbbe06c4 100644
--- a/lib/system.h
+++ b/lib/system.h
@@ -64,12 +64,12 @@ void error(int status, int errnum, const char *format, ...);
 exit (EXIT_FAILURE); \
   } while (0)
 
-#if __BYTE_ORDER == __LITTLE_ENDIAN
+#if BYTE_ORDER == LITTLE_ENDIAN
 # define LE32(n)   (n)
 # define LE64(n)   (n)
 # define BE32(n)   bswap_32 (n)
 # define BE64(n)   bswap_64 (n)
-#elif __BYTE_ORDER == __BIG_ENDIAN
+#elif BYTE_ORDER == BIG_ENDIAN
 # define BE32(n)   (n)
 # define BE64(n)   (n)
 # define LE32(n)   bswap_32 (n)
diff --git a/libcpu/i386_disasm.c b/libcpu/i386_disasm.c
index fd7340cc..40475b81 100644
--- a/libcpu/i386_disasm.c
+++ b/libcpu/i386_disasm.c
@@ -44,7 +44,7 @@
 
 #include "../libebl/libeblP.h"
 
-#define MACHINE_ENCODING __LITTLE_ENDIAN
+#define MACHINE_ENCODING LITTLE_ENDIAN
 #include "memory-access.h"
 
 
diff --git a/libcpu/memory-access.h b/libcpu/memory-access.h
index 779825fa..3b6ca19b 100644
--- a/libcpu/memory-access.h
+++ b/libcpu/memory-access.h
@@ -41,7 +41,7 @@
 #ifndef MACHINE_ENCODING
 # error "MACHINE_ENCODING needs to be defined"
 #endif
-#if MACHINE_ENCODING != __BIG_ENDIAN && MACHINE_ENCODING != __LITTLE_ENDIAN
+#if MACHINE_ENCODING != BIG_ENDIAN && MACHINE_ENCODING != LITTLE_ENDIAN
 # error "MACHINE_ENCODING must signal either big or little endian"
 #endif
 
@@ -51,31 +51,31 @@
 #if ALLOW_UNALIGNED
 
 # define read_2ubyte_unaligned(Addr) \
-  (unlikely (MACHINE_ENCODING != __BYTE_ORDER)   \
+  (unlikely (MACHINE_ENCODING != BYTE_ORDER) \
? bswap_16 (*((const uint16_t *) (Addr))) \
: *((const uint16_t *) (Addr)))
 # define read_2sbyte_unaligned(Addr) \
-  (unlikely (MACHINE_ENCODING != __BYTE_ORDER)   \
+  (unlikely (MACHINE_ENCODING != BYTE_ORDER) \
? (int16_t) bswap_16 (*((const int16_t *) (Addr)))\
: *((const int16_t *) (Addr)))
 
 # define read_4ubyte_unaligned_noncvt(Addr) \
*((const uint32_t *) (Addr))
 # define read_4ubyte_unaligned(Addr) \
-  (unlikely (MACHINE_ENCODING != __BYTE_ORDER)   \
+  (unlikely (MACHINE_ENCODING != BYTE_ORDER) \
? bswap_32 (*((const uint32_t *) (Addr))) \
: *((const uint32_t *) (Addr)))
 # define read_4sbyte_unaligned(Addr) \
-  (unlikely (MACHINE_ENCODING != __BYTE_ORDER)   \
+  (unlikely (MACHINE_ENCODING != BYTE_ORDER) \
? (int32_t) bswap_32 (*((const int32_t *) (Addr)))\
: *((const int32_t *) (Addr)))
 
 # define read_8ubyte_unaligned(Addr) \
-  (unlikely (MACHINE_ENCODING != __BYTE_ORDER)   \
+  (unlikely (MACHINE_ENCODING != BYTE_ORDER) \
? bswap_64 (*((const uint64_t *) (Addr))) \
: *((const uint64_t *) (Addr)))
 # define read_8sbyte_unaligned(Addr) \
-  (unlikely (MACHINE_ENCODING != __BYTE_ORDER)   \
+  (unlikely (MACHINE_ENCODING != BYTE_ORDER) \
? (int64_t) bswap_64 (*((const int64_t *) (Addr)))\
: *((const int64_t *) (Addr)))
 
@@ -96,7 +96,7 @@ static inline uint16_t
 read_2ubyte_unaligned (const void *p)
 {
   const union unaligned *up = p;
-  if (MACHINE_ENCODING != __BYTE_ORDER)
+  if (MACHINE_ENCODING != BYTE_ORDER)
 return bswap_16 (up->u2);
   return up->u2;
 }
@@ -104,7 +104,7 @@ static inline int16_t
 read_2sbyte_unaligned (const void *p)
 {
   const union unaligned *up = p;
-  if (MACHINE_ENCODING != __BYTE_ORDER)
+  if (MACHINE_ENCODING != BYTE_ORDER)
 return (int16_t) bswap_16 (up->u2);
   return up->s2;
 }
@@ -119,7 +119,7 @@ static inline uint32_t
 read_4ubyte_unaligned (const void *p)
 {
   const union unaligned *up = p;
-  if (MACHINE_ENCODING != __BYTE_ORDER)
+  if (MACHINE_ENCODING != BYTE_ORDER)
 return bswap_32 (up->u4);
   return up->u4;
 }
@@ -127,7 +127,7 @@ static inline int32_t
 read_4sbyte_unaligned (const void *p)
 {
   const union unaligned *up = p;
-  if (MACHINE_ENCODING != __BYTE_ORDER)
+  if (MACHINE_ENCODING != BYTE_ORDER

[PATCH 7/7] Add CMake build files

2022-09-20 Thread Yonggang Luo via Elfutils-devel
Signed-off-by: Yonggang Luo 
---
 .gitignore   |   1 +
 CMakeLists.txt   |  47 +
 libelf/CMakeLists.txt| 192 +++
 libelf/config-cmake/config.h |  27 +
 4 files changed, 267 insertions(+)
 create mode 100644 CMakeLists.txt
 create mode 100644 libelf/CMakeLists.txt
 create mode 100644 libelf/config-cmake/config.h

diff --git a/.gitignore b/.gitignore
index 8bcd88d7..ca06 100644
--- a/.gitignore
+++ b/.gitignore
@@ -21,6 +21,7 @@ Makefile.in
 /INSTALL
 /aclocal.m4
 /autom4te.*
+/build
 /config.cache
 /config.h
 /config.h.in
diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644
index ..11ad5c13
--- /dev/null
+++ b/CMakeLists.txt
@@ -0,0 +1,47 @@
+# Copyright (C) 2022 Yonggang Luo
+# This file is part of elfutils.
+#
+# This file is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# elfutils is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see .
+
+cmake_minimum_required(VERSION 3.16)
+project(libelf
+LANGUAGES C
+VERSION 0.187
+)
+
+add_subdirectory(libelf)
+
+include(GNUInstallDirs)
+
+if (LIBELF_INSTALL)
+# Install the targets
+install(TARGETS elf
+ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
+PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/libelf
+)
+set(VERSION ${CMAKE_PROJECT_VERSION})
+set(prefix ${CMAKE_INSTALL_PREFIX})
+set(exec_prefix "\${prefix}")
+set(libdir "\${exec_prefix}/lib")
+set(includedir "\${prefix}/include/libelf")
+configure_file(
+${CMAKE_CURRENT_SOURCE_DIR}/config/libelf.pc.in
+${CMAKE_CURRENT_BINARY_DIR}/libelf.pc
+@ONLY
+)
+install(
+FILES ${CMAKE_CURRENT_BINARY_DIR}/libelf.pc
+DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig
+)
+endif()
diff --git a/libelf/CMakeLists.txt b/libelf/CMakeLists.txt
new file mode 100644
index ..dc8f9291
--- /dev/null
+++ b/libelf/CMakeLists.txt
@@ -0,0 +1,192 @@
+# Copyright (C) 2022 Yonggang Luo
+# This file is part of elfutils.
+#
+# This file is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# elfutils is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see .
+
+add_library(elf
+elf_begin.c
+elf_clone.c
+elf_cntl.c
+elf_compress_gnu.c
+elf_compress.c
+elf_end.c
+elf_error.c
+elf_fill.c
+elf_flagdata.c
+elf_flagehdr.c
+elf_flagelf.c
+elf_flagphdr.c
+elf_flagscn.c
+elf_flagshdr.c
+elf_getarhdr.c
+elf_getaroff.c
+elf_getarsym.c
+elf_getbase.c
+elf_getdata_rawchunk.c
+elf_getdata.c
+elf_getident.c
+elf_getphdrnum.c
+elf_getscn.c
+elf_getshdrnum.c
+elf_getshdrstrndx.c
+elf_gnu_hash.c
+elf_hash.c
+elf_kind.c
+elf_memory.c
+elf_ndxscn.c
+elf_newdata.c
+elf_newscn.c
+elf_next.c
+elf_nextscn.c
+elf_rand.c
+elf_rawdata.c
+elf_rawfile.c
+elf_readall.c
+elf_scnshndx.c
+elf_strptr.c
+elf_update.c
+elf_version.c
+elf32_checksum.c
+elf32_fsize.c
+elf32_getchdr.c
+elf32_getehdr.c
+elf32_getphdr.c
+elf32_getshdr.c
+elf32_newehdr.c
+elf32_newphdr.c
+elf32_offscn.c
+elf32_updatefile.c
+elf32_updatenull.c
+elf32_xlatetof.c
+elf32_xlatetom.c
+elf64_checksum.c
+elf64_fsize.c
+elf64_getchdr.c
+elf64_getehdr.c
+elf64_getphdr.c
+elf64_getshdr.c
+elf64_newehdr.c
+elf64_newphdr.c
+elf64_offscn.c
+elf64_updatefile.c
+elf64_updatenull.c
+elf64_xlatetof.c
+elf64_xlatetom.c
+gelf_checksum.c
+gelf_fsize.c
+gelf_getauxv.c
+gelf_getchdr.c
+gelf_getclass.c
+gelf_getdyn.c
+gelf_getehdr.c
+gelf_getlib.c
+gelf_getmove.c
+gelf_getnote.c
+gelf_getphdr.c
+gelf_getrel.c
+gelf_getrela.c
+gelf_getshdr.c
+gelf_getsym.c
+gelf_getsyminfo.c
+gelf_getsymshndx.c
+gelf_getverdaux.c
+gelf_getverdef.c
+gelf_getvernaux.c
+gelf_getverneed.c

[PATCH] libdwfl: add dwfl_report_offline_memory

2022-09-20 Thread Aleksei Vetrov via Elfutils-devel
From: Aleksei Vetrov 

This method allows to read and report ELF from memory instead of opening
a file. That way arbitrary memory can be worked with, e.g. when coming
from a stream without the need to persist.

Another useful application is for fuzzing, because fuzzers might be able
to track accesses to the memory and change the fuzzer input to cover
more edge cases through more targeted input. Hence, add a new function
along with a test case.

Signed-off-by: Aleksei Vetrov 
---
 ChangeLog   |  4 +
 libdw/libdw.map |  1 +
 libdwfl/libdwfl.h   |  4 +
 libdwfl/libdwflP.h  |  6 ++
 libdwfl/offline.c   | 26 +++
 libdwfl/open.c  | 30 +++-
 tests/Makefile.am   |  4 +
 tests/dwfl-report-offline-memory.c  | 97 +
 tests/run-dwfl-report-offline-memory.sh | 26 +++
 9 files changed, 194 insertions(+), 4 deletions(-)
 create mode 100644 tests/dwfl-report-offline-memory.c
 create mode 100755 tests/run-dwfl-report-offline-memory.sh

diff --git a/ChangeLog b/ChangeLog
index 5421f5b8..c3a1948a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2022-09-13  Aleksei Vetrov  
+
+   * NEWS (libdwfl): Add dwfl_report_offline_memory.
+
 2022-04-28  Di Chen  
 
* NEWS: Add readefl -D, --use-dynamic.
diff --git a/libdw/libdw.map b/libdw/libdw.map
index 8f393438..ff3b7d20 100644
--- a/libdw/libdw.map
+++ b/libdw/libdw.map
@@ -137,6 +137,7 @@ ELFUTILS_0.122 {
 dwfl_report_end;
 dwfl_report_module;
 dwfl_report_offline;
+dwfl_report_offline_memory;
 dwfl_standard_argp;
 dwfl_standard_find_debuginfo;
 dwfl_version;
diff --git a/libdwfl/libdwfl.h b/libdwfl/libdwfl.h
index edb537c2..9114f7f0 100644
--- a/libdwfl/libdwfl.h
+++ b/libdwfl/libdwfl.h
@@ -159,6 +159,10 @@ extern Dwfl_Module *dwfl_report_elf (Dwfl *dwfl, const 
char *name,
 extern Dwfl_Module *dwfl_report_offline (Dwfl *dwfl, const char *name,
 const char *file_name, int fd);
 
+/* Similar, but report ELF from memory region.  */
+extern Dwfl_Module *dwfl_report_offline_memory (Dwfl *dwfl, const char *name,
+   const char *file_name,
+   char *data, size_t size);
 
 /* Finish reporting the current set of modules to the library.
If REMOVED is not null, it's called for each module that
diff --git a/libdwfl/libdwflP.h b/libdwfl/libdwflP.h
index a2949e74..011b5de9 100644
--- a/libdwfl/libdwflP.h
+++ b/libdwfl/libdwflP.h
@@ -631,6 +631,11 @@ extern Dwfl_Error __libdw_open_file (int *fdp, Elf **elfp,
 bool close_on_fail, bool archive_ok)
   internal_function;
 
+/* Same as __libdw_open_file, but opens Elf handle from memory region.  */
+extern Dwfl_Error __libdw_open_elf_memory (char *data, size_t size, Elf **elfp,
+  bool archive_ok)
+  internal_function;
+
 /* Same as __libdw_open_file, but never closes the given file
descriptor and ELF_K_AR is always an acceptable type.  */
 extern Dwfl_Error __libdw_open_elf (int fd, Elf **elfp) internal_function;
@@ -760,6 +765,7 @@ INTDECL (dwfl_report_begin_add)
 INTDECL (dwfl_report_module)
 INTDECL (dwfl_report_segment)
 INTDECL (dwfl_report_offline)
+INTDECL (dwfl_report_offline_memory)
 INTDECL (dwfl_report_end)
 INTDECL (dwfl_build_id_find_elf)
 INTDECL (dwfl_build_id_find_debuginfo)
diff --git a/libdwfl/offline.c b/libdwfl/offline.c
index 58ba4c36..499663e3 100644
--- a/libdwfl/offline.c
+++ b/libdwfl/offline.c
@@ -1,6 +1,7 @@
 /* Recover relocatibility for addresses computed from debug information.
Copyright (C) 2005-2009, 2012 Red Hat, Inc.
Copyright (C) 2022 Mark J. Wielaard 
+   Copyright (C) 2022 Google LLC
This file is part of elfutils.
 
This file is free software; you can redistribute it and/or modify
@@ -252,6 +253,7 @@ process_archive (Dwfl *dwfl, const char *name, const char 
*file_name, int fd,
 
 {
   Dwfl_Module *mod = NULL;
+  /* elf_begin supports opening archives even with fd == -1 passed.  */
   Elf *member = elf_begin (fd, ELF_C_READ_MMAP_PRIVATE, archive);
   if (unlikely (member == NULL)) /* Empty archive.  */
 {
@@ -320,3 +322,27 @@ dwfl_report_offline (Dwfl *dwfl, const char *name,
   return __libdwfl_report_offline (dwfl, name, file_name, fd, closefd, NULL);
 }
 INTDEF (dwfl_report_offline)
+
+Dwfl_Module *
+dwfl_report_offline_memory (Dwfl *dwfl, const char *name,
+   const char *file_name, char *data, size_t size)
+{
+  if (dwfl == NULL)
+return NULL;
+
+  Elf *elf;
+  Dwfl_Error error = __libdw_open_elf_memory (data, size, &elf, true);
+  if (error != DWFL_E_NOERROR)
+{
+  __libdwfl_seterrno (error);
+  return NULL;
+}
+  /* It is ok to pass fd == -1 here, because libelf uses it as a value for
+ "no f