[PATCH v2 01/16] ignore build directory

2022-12-17 Thread Yonggang Luo via Elfutils-devel
This is for in in three building and won't affect IDE
for example
mkdir build && cd build && ../configure && make install

Signed-off-by: Yonggang Luo 
---
 .gitignore | 1 +
 ChangeLog  | 4 
 2 files changed, 5 insertions(+)

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/ChangeLog b/ChangeLog
index 52efca04..35a37343 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2022-12-17  Yonggang Luo 
+
+   * .gitignore: Ignore build directory
+
 2022-11-02  Mark Wielaard  
 
* configure.ac (AC_INIT): Set version to 0.188.
-- 
2.36.1.windows.1



[PATCH v2 02/16] move platform depended include into system.h of libebl

2022-12-17 Thread Yonggang Luo via Elfutils-devel
Because all source in libebl #include , so #include  in
libeblP.h is enough, there is multiple memory-access.h file, so use relative 
path to
include it properly,

Signed-off-by: Yonggang Luo 
-#include 
-#include 
 #include 
 #include 
 #include 
diff --git a/libebl/eblcorenote.c b/libebl/eblcorenote.c
index 7fab3974..3d4c8a92 100644
--- a/libebl/eblcorenote.c
+++ b/libebl/eblcorenote.c
@@ -31,8 +31,6 @@
 #endif
 
 #include 
-#include 
-#include 
 #include 
 #include 
 #include 
diff --git a/libebl/ebldynamictagname.c b/libebl/ebldynamictagname.c
index 5d4a3a58..7a430f49 100644
--- a/libebl/ebldynamictagname.c
+++ b/libebl/ebldynamictagname.c
@@ -34,7 +34,6 @@
 #include 
 #include 
 #include 
-#include "system.h"
 
 
 const char *
diff --git a/libebl/eblobjnote.c b/libebl/eblobjnote.c
index 5a7c5c62..0bb56c02 100644
--- a/libebl/eblobjnote.c
+++ b/libebl/eblobjnote.c
@@ -37,10 +37,10 @@
 #include 
 #include 
 
-#include "common.h"
-#include "libelfP.h"
-#include "libdwP.h"
-#include "memory-access.h"
+#include "../libelf/common.h"
+#include "../libelf/libelfP.h"
+#include "../libdw/libdwP.h"
+#include "../libdw/memory-access.h"
 
 
 void
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 02f80653..6e884ea0 100644
--- a/libebl/eblopenbackend.c
+++ b/libebl/eblopenbackend.c
@@ -37,7 +37,6 @@
 #include 
 #include 
 
-#include 
 #include 
 
 Ebl *i386_init (Elf *, GElf_Half, Ebl *);
diff --git a/libebl/libeblP.h b/libebl/libeblP.h
index c408ed97..9dfd60de 100644
--- a/libebl/libeblP.h
+++ b/libebl/libeblP.h
@@ -33,6 +33,7 @@
 #include 
 #include 
 
+#include 
 
 /* Backend handle.  */
 struct ebl
-- 
2.36.1.windows.1



[PATCH v2 00/16] Patches for building with mingw/gcc msvc/clang-cl

2022-12-17 Thread Yonggang Luo via Elfutils-devel
I split it into small patches for easier to review
Changes v1->v2:
  The reviews are applied

Yonggang Luo (16):
  ignore build directory
  move platform depended include into system.h of libebl
  Use configure to detect HAVE_DECL_MMAP and use it for system doesn't
provide sys/mman.h
  Fixes usage of basename about prototype differences
  libcpu: Remove the need of NMNES by using enum
  libcpu: Use __asm instead asm that can be recognized by both clang-cl
and gcc
  libdw: Fixes compile of dwarf_whatattr.c and dwarf_whatform.c
  lib: Implement error properly even when not HAVE_ERR_H
  libelf: uid_t, gid_t and mode_t are not comes with msvcrt, so using
long/unsigned long instead on win32
  libasm: stdio_ext.h are not present on win32
  libebl/libdwelf: define ssize_t and pid_t for MSVC within installed
header libdwelf.h and libebl.h
  libasm/debuginfod: fchmod doesn't present on win32
  lib: isatty is not available on windows
  Add function sys_get_page_size to replace platform dependent sysconf
(_SC_PAGESIZE)
  libelf: F_GETFD may not predefined with msvc/mingw, guard the usage of
it
  lib: Use HAVE_LIBINTL_H to guard #include 

 .gitignore |  1 +
 ChangeLog  |  4 ++
 configure.ac   |  3 +
 debuginfod/debuginfod-client.c |  4 +-
 lib/Makefile.am|  2 +-
 lib/color.c|  5 ++
 lib/crc32_file.c   |  6 +-
 lib/error.c| 16 -
 lib/eu-config.h|  7 ++
 libdw/dwarf_whatform.c => lib/system.c | 90 ++
 lib/system.h   |  9 ++-
 libasm/asm_begin.c |  7 +-
 libasm/asm_end.c   |  2 +
 libcpu/Makefile.am |  2 +-
 libcpu/i386_disasm.c   | 16 +
 libcpu/i386_mne.h  | 36 +++
 libcpu/i386_parse.y|  9 +--
 libdw/dwarf_begin_elf.c|  2 +-
 libdw/dwarf_getsrc_file.c  |  2 +-
 libdw/dwarf_whatattr.c |  9 ++-
 libdw/dwarf_whatform.c |  9 ++-
 libdwelf/dwelf_strtab.c|  2 +-
 libdwelf/libdwelf.h|  5 ++
 libdwfl/dwfl_module_getsrc_file.c  |  2 +-
 libdwfl/dwfl_segment_report_module.c   |  2 +-
 libdwfl/find-debuginfo.c   |  6 +-
 libdwfl/link_map.c |  2 +-
 libdwfl/linux-kernel-modules.c |  2 +-
 libdwfl/linux-proc-maps.c  |  2 +-
 libebl/eblauxvinfo.c   |  2 -
 libebl/eblcorenote.c   |  2 -
 libebl/ebldynamictagname.c |  1 -
 libebl/eblobjnote.c|  8 +--
 libebl/eblobjnotetypename.c|  2 -
 libebl/eblopenbackend.c|  1 -
 libebl/libebl.h|  5 ++
 libebl/libeblP.h   |  1 +
 libelf/elf32_updatefile.c  |  5 +-
 libelf/elf_begin.c | 12 +++-
 libelf/elf_end.c   |  2 +
 libelf/elf_update.c|  5 +-
 libelf/libelf.h|  6 ++
 src/addr2line.c|  4 +-
 src/ar.c   |  2 +-
 src/nm.c   |  4 +-
 src/ranlib.c   |  2 +-
 src/stack.c|  2 +-
 src/strings.c  |  2 +-
 src/strip.c|  2 +-
 49 files changed, 215 insertions(+), 119 deletions(-)
 copy libdw/dwarf_whatform.c => lib/system.c (66%)
 create mode 100644 libcpu/i386_mne.h

-- 
2.36.1.windows.1



[PATCH v2 03/16] Use configure to detect HAVE_DECL_MMAP and use it for system doesn't provide sys/mman.h

2022-12-17 Thread Yonggang Luo via Elfutils-devel
Signed-off-by: Yonggang Luo 
---
 configure.ac  | 1 +
 lib/crc32_file.c  | 4 ++--
 lib/system.h  | 2 ++
 libelf/elf32_updatefile.c | 3 ++-
 libelf/elf_begin.c| 5 -
 libelf/elf_end.c  | 2 ++
 libelf/elf_update.c   | 5 -
 7 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/configure.ac b/configure.ac
index 59be27ac..b84623fe 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/crc32_file.c b/lib/crc32_file.c
index f7607d0b..66833702 100644
--- a/lib/crc32_file.c
+++ b/lib/crc32_file.c
@@ -32,9 +32,7 @@
 
 #include "libeu.h"
 #include 
-#include 
 #include 
-#include 
 #include "system.h"
 
 int
@@ -45,6 +43,7 @@ crc32_file (int fd, uint32_t *resp)
   off_t off = 0;
   ssize_t count;
 
+#if HAVE_DECL_MMAP
   struct stat st;
   if (fstat (fd, &st) == 0)
 {
@@ -78,6 +77,7 @@ crc32_file (int fd, uint32_t *resp)
  munmap (mapped, mapsize);
}
 }
+#endif
 
   while ((count = TEMP_FAILURE_RETRY (pread (fd, buffer, sizeof buffer,
 off))) > 0)
diff --git a/lib/system.h b/lib/system.h
index bbbe06c4..561d3e03 100644
--- a/lib/system.h
+++ b/lib/system.h
@@ -42,7 +42,9 @@
 /* System dependend headers */
 #include 
 #include 
+#if HAVE_DECL_MMAP
 #include 
+#endif
 #include 
 #include 
 
diff --git a/libelf/elf32_updatefile.c b/libelf/elf32_updatefile.c
index 46afa1f4..8229fd26 100644
--- a/libelf/elf32_updatefile.c
+++ b/libelf/elf32_updatefile.c
@@ -96,7 +96,7 @@ sort_sections (Elf_Scn **scns, Elf_ScnList *list)
   qsort (scns, scnp - scns, sizeof (*scns), compare_sections);
 }
 
-
+#if HAVE_DECL_MMAP
 static inline void
 fill_mmap (size_t offset, char *last_position, char *scn_start,
char *const shdr_start, char *const shdr_end)
@@ -482,6 +482,7 @@ __elfw2(LIBELFBITS,updatemmap) (Elf *elf, int change_bo, 
size_t shnum)
 
   return 0;
 }
+#endif
 
 
 /* Size of the buffer we use to generate the blocks of fill bytes.  */
diff --git a/libelf/elf_begin.c b/libelf/elf_begin.c
index fe8c640a..6d31882e 100644
--- a/libelf/elf_begin.c
+++ b/libelf/elf_begin.c
@@ -645,10 +645,12 @@ static struct Elf *
 read_file (int fildes, int64_t offset, size_t maxsize,
   Elf_Cmd cmd, Elf *parent)
 {
+#if HAVE_DECL_MMAP
   void *map_address = NULL;
   int use_mmap = (cmd == ELF_C_READ_MMAP || cmd == ELF_C_RDWR_MMAP
  || cmd == ELF_C_WRITE_MMAP
  || cmd == ELF_C_READ_MMAP_PRIVATE);
+#endif
 
   if (parent == NULL)
 {
@@ -669,7 +671,7 @@ read_file (int fildes, int64_t offset, size_t maxsize,
   /* The parent is already loaded.  Use it.  */
   assert (maxsize != ~((size_t) 0));
 }
-
+#if HAVE_DECL_MMAP
   if (use_mmap)
 {
   if (parent == NULL)
@@ -713,6 +715,7 @@ read_file (int fildes, int64_t offset, size_t maxsize,
 
   return result;
 }
+#endif
 
   /* Otherwise we have to do it the hard way.  We read as much as necessary
  from the file whenever we need information which is not available.  */
diff --git a/libelf/elf_end.c b/libelf/elf_end.c
index 5c451f36..8023b216 100644
--- a/libelf/elf_end.c
+++ b/libelf/elf_end.c
@@ -222,8 +222,10 @@ elf_end (Elf *elf)
   /* The file was read or mapped for this descriptor.  */
   if ((elf->flags & ELF_F_MALLOCED) != 0)
free (elf->map_address);
+#if HAVE_DECL_MMAP
   else if ((elf->flags & ELF_F_MMAPPED) != 0)
munmap (elf->map_address, elf->maximum_size);
+#endif
 }
 
   rwlock_unlock (elf->lock);
diff --git a/libelf/elf_update.c b/libelf/elf_update.c
index 56af3a1c..e81eb6c9 100644
--- a/libelf/elf_update.c
+++ b/libelf/elf_update.c
@@ -64,7 +64,7 @@ write_file (Elf *elf, int64_t size, int change_bo, size_t 
shnum)
   __libelf_seterrno (ELF_E_WRITE_ERROR);
   return -1;
 }
-
+#if HAVE_DECL_MMAP
   /* Try to map the file if this isn't done yet.  */
   if (elf->map_address == NULL && elf->cmd == ELF_C_WRITE_MMAP)
 {
@@ -125,6 +125,7 @@ write_file (Elf *elf, int64_t size, int change_bo, size_t 
shnum)
size = -1;
 }
   else
+#endif
 {
   /* The file is not mmaped.  */
   if ((class == ELFCLASS32
@@ -145,6 +146,7 @@ write_file (Elf *elf, int64_t size, int change_bo, size_t 
shnum)
   size = -1;
 }
 
+#if HAVE_DECL_MMAP
   /* POSIX says that ftruncate and write may clear the S_ISUID and S_ISGID
  mode bits.  So make sure we restore them afterwards if they were set.
  This is not atomic if someone else chmod's the file while we operate.  */
@@ -156,6 +158,7 @@ write_file (Elf *elf, int64_t size, int change_bo, size_t 
shnum)
   __libelf_seterrno (ELF_E_WRITE_ERROR);
   s

[PATCH v2 04/16] Fixes usage of basename about prototype differences

2022-12-17 Thread Yonggang Luo via Elfutils-devel
Signed-off-by: Yonggang Luo 
---
 libdw/dwarf_getsrc_file.c| 2 +-
 libdwfl/dwfl_module_getsrc_file.c| 2 +-
 libdwfl/dwfl_segment_report_module.c | 2 +-
 libdwfl/find-debuginfo.c | 6 +++---
 libdwfl/link_map.c   | 2 +-
 src/addr2line.c  | 4 ++--
 src/nm.c | 4 ++--
 src/stack.c  | 2 +-
 src/strip.c  | 2 +-
 9 files changed, 13 insertions(+), 13 deletions(-)

diff --git a/libdw/dwarf_getsrc_file.c b/libdw/dwarf_getsrc_file.c
index 5289c7da..884fea32 100644
--- a/libdw/dwarf_getsrc_file.c
+++ b/libdw/dwarf_getsrc_file.c
@@ -98,7 +98,7 @@ dwarf_getsrc_file (Dwarf *dbg, const char *fname, int lineno, 
int column,
  /* Match the name with the name the user provided.  */
  const char *fname2 = line->files->info[lastfile].name;
  if (is_basename)
-   lastmatch = strcmp (basename (fname2), fname) == 0;
+   lastmatch = strcmp (basename ((char *)fname2), fname) == 0;
  else
lastmatch = strcmp (fname2, fname) == 0;
}
diff --git a/libdwfl/dwfl_module_getsrc_file.c 
b/libdwfl/dwfl_module_getsrc_file.c
index cea2ba41..6daf29d6 100644
--- a/libdwfl/dwfl_module_getsrc_file.c
+++ b/libdwfl/dwfl_module_getsrc_file.c
@@ -103,7 +103,7 @@ dwfl_module_getsrc_file (Dwfl_Module *mod,
{
  /* Match the name with the name the user provided.  */
  lastfile = file;
- lastmatch = !strcmp (is_basename ? basename (file) : file,
+ lastmatch = !strcmp (is_basename ? basename ((char *)file) : 
file,
   fname);
}
}
diff --git a/libdwfl/dwfl_segment_report_module.c 
b/libdwfl/dwfl_segment_report_module.c
index 19fa6ded..5342648a 100644
--- a/libdwfl/dwfl_segment_report_module.c
+++ b/libdwfl/dwfl_segment_report_module.c
@@ -719,7 +719,7 @@ dwfl_segment_report_module (Dwfl *dwfl, int ndx, const char 
*name,
  bias += fixup;
  if (module->name[0] != '\0')
{
- name = basename (module->name);
+ name = basename ((char *)module->name);
  name_is_final = true;
}
  break;
diff --git a/libdwfl/find-debuginfo.c b/libdwfl/find-debuginfo.c
index 7f7ab632..a9b7be3d 100644
--- a/libdwfl/find-debuginfo.c
+++ b/libdwfl/find-debuginfo.c
@@ -164,7 +164,7 @@ find_debuginfo_in_path (Dwfl_Module *mod, const char 
*file_name,
 {
   bool cancheck = debuglink_crc != (GElf_Word) 0;
 
-  const char *file_basename = file_name == NULL ? NULL : basename (file_name);
+  const char *file_basename = file_name == NULL ? NULL : basename ((char 
*)file_name);
   char *localname = NULL;
 
   /* We invent a debuglink .debug name if NULL, but then want to try the
@@ -278,7 +278,7 @@ find_debuginfo_in_path (Dwfl_Module *mod, const char 
*file_name,
  else
{
  subdir = NULL;
- file = basename (debuglink_file);
+ file = basename ((char *)debuglink_file);
}
  try_file_basename = debuglink_null;
  break;
@@ -306,7 +306,7 @@ find_debuginfo_in_path (Dwfl_Module *mod, const char 
*file_name,
if (mod->dw != NULL && (p[0] == '\0' || p[0] == '/'))
  {
fd = try_open (&main_stat, dir, ".dwz",
-  basename (file), &fname);
+  basename ((char *)file), &fname);
if (fd < 0)
  {
if (errno != ENOENT && errno != ENOTDIR)
diff --git a/libdwfl/link_map.c b/libdwfl/link_map.c
index 7ec7eca1..403d4ee5 100644
--- a/libdwfl/link_map.c
+++ b/libdwfl/link_map.c
@@ -469,7 +469,7 @@ report_r_debug (uint_fast8_t elfclass, uint_fast8_t elfdata,
  if (r_debug_info_module == NULL)
{
  // XXX hook for sysroot
- mod = __libdwfl_report_elf (dwfl, basename (name),
+ mod = __libdwfl_report_elf (dwfl, basename ((char 
*)name),
  name, fd, elf, base,
  true, true);
  if (mod != NULL)
diff --git a/src/addr2line.c b/src/addr2line.c
index 7768b266..3abf1d7a 100644
--- a/src/addr2line.c
+++ b/src/addr2line.c
@@ -381,7 +381,7 @@ print_dwarf_function (Dwfl_Module *mod, Dwarf_Addr addr)
  if (file == NULL)
file = "???";
  else if (only_basenames)
-   file = basename (file);
+   file = basename ((char *)file);
  else if (use_comp_dir && file[0] != '/')
{
  const char *const *dirs;
@@ -564,7 +564,7 @@ print_src (const char *src, int line

[PATCH v2 05/16] libcpu: Remove the need of NMNES by using enum

2022-12-17 Thread Yonggang Luo via Elfutils-devel
Signed-off-by: Yonggang Luo 
---
 libcpu/Makefile.am   |  2 +-
 libcpu/i386_disasm.c | 14 +-
 libcpu/i386_mne.h| 36 
 libcpu/i386_parse.y  |  9 +++--
 4 files changed, 41 insertions(+), 20 deletions(-)
 create mode 100644 libcpu/i386_mne.h

diff --git a/libcpu/Makefile.am b/libcpu/Makefile.am
index 57d0a164..259ed838 100644
--- a/libcpu/Makefile.am
+++ b/libcpu/Makefile.am
@@ -92,7 +92,7 @@ libeu = ../lib/libeu.a
 i386_lex_CFLAGS = -Wno-unused-label -Wno-unused-function -Wno-sign-compare \
  -Wno-implicit-fallthrough
 i386_parse.o: i386_parse.c i386.mnemonics
-i386_parse_CFLAGS = -DNMNES="`wc -l < i386.mnemonics`"
+i386_parse_CFLAGS =
 i386_lex.o: i386_parse.h
 i386_gendis_LDADD = $(libeu) -lm $(obstack_LIBS)
 
diff --git a/libcpu/i386_disasm.c b/libcpu/i386_disasm.c
index 599d1654..c34f03d6 100644
--- a/libcpu/i386_disasm.c
+++ b/libcpu/i386_disasm.c
@@ -46,10 +46,7 @@
 #define MACHINE_ENCODING LITTLE_ENDIAN
 #include "memory-access.h"
 
-
-#ifndef MNEFILE
-# define MNEFILE "i386.mnemonics"
-#endif
+#include "i386_mne.h"
 
 #define MNESTRFIELD(line) MNESTRFIELD1 (line)
 #define MNESTRFIELD1(line) str##line
@@ -71,15 +68,6 @@ static const union mnestr_t
 }
   };
 
-/* The index can be stored in the instrtab.  */
-enum
-  {
-#define MNE(name) MNE_##name,
-#include MNEFILE
-#undef MNE
-MNE_INVALID
-  };
-
 static const unsigned short int mneidx[] =
   {
 #define MNE(name) \
diff --git a/libcpu/i386_mne.h b/libcpu/i386_mne.h
new file mode 100644
index ..41dacf61
--- /dev/null
+++ b/libcpu/i386_mne.h
@@ -0,0 +1,36 @@
+/* Compute hash value for given string according to ELF standard.
+   Copyright (C) 1995-2015 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library 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
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   .  */
+
+#ifndef _I386_MNE_H
+#define _I386_MNE_H1
+
+#ifndef MNEFILE
+# define MNEFILE "i386.mnemonics"
+#endif
+
+/* The index can be stored in the instrtab.  */
+enum
+  {
+#define MNE(name) MNE_##name,
+#include MNEFILE
+#undef MNE
+MNE_INVALID,
+MNE_COUNT = MNE_INVALID,
+  };
+
+#endif /* i386_mne.h */
diff --git a/libcpu/i386_parse.y b/libcpu/i386_parse.y
index d2236d59..459684c6 100644
--- a/libcpu/i386_parse.y
+++ b/libcpu/i386_parse.y
@@ -46,6 +46,8 @@
 #include 
 #include 
 
+#include "i386_mne.h"
+
 #define obstack_chunk_alloc xmalloc
 #define obstack_chunk_free free
 
@@ -1107,11 +1109,6 @@ print_op_fct (const void *nodep, VISIT value,
 }
 }
 
-
-#if NMNES < 2
-# error "bogus NMNES value"
-#endif
-
 static void
 instrtable_out (void)
 {
@@ -1123,7 +1120,7 @@ instrtable_out (void)
   fprintf (outfile, "#define MNEMONIC_BITS %zu\n", best_mnemonic_bits);
 #else
   fprintf (outfile, "#define MNEMONIC_BITS %ld\n",
-  lrint (ceil (log2 (NMNES;
+  lrint (ceil (log2 (MNE_COUNT;
 #endif
   fprintf (outfile, "#define SUFFIX_BITS %d\n", nbitsuf);
   for (int i = 0; i < 3; ++i)
-- 
2.36.1.windows.1



[PATCH v2 06/16] libcpu: Use __asm instead asm that can be recognized by both clang-cl and gcc

2022-12-17 Thread Yonggang Luo via Elfutils-devel
This block of code can not be removed. As it's contains a goto label
enomem that been used elsewhere

Signed-off-by: Yonggang Luo 
---
 libcpu/i386_disasm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libcpu/i386_disasm.c b/libcpu/i386_disasm.c
index c34f03d6..44bf7d37 100644
--- a/libcpu/i386_disasm.c
+++ b/libcpu/i386_disasm.c
@@ -468,7 +468,7 @@ i386_disasm (Ebl *ebl __attribute__((unused)),
 
  /* gcc is not clever enough to see the following variables
 are not used uninitialized.  */
- asm (""
+ __asm (""
   : "=mr" (opoff), "=mr" (correct_prefix), "=mr" (codep),
 "=mr" (next_curr), "=mr" (len));
}
-- 
2.36.1.windows.1



[PATCH v2 07/16] libdw: Fixes compile of dwarf_whatattr.c and dwarf_whatform.c

2022-12-17 Thread Yonggang Luo via Elfutils-devel
If __OPTIMIZE__ is defined, then compile  dwarf_whatattr.c and dwarf_whatform.c
will cause symbol conflict between
dwarf_whatattr.c and libdw.h,
dwarf_whatform.c and libdw.h,

So always undefined __OPTIMIZE__ when compiling these two files

The error message is:
dwarf_whatform.c

[build] C:\work\xemu\elfutils\libdw\dwarf_whatform.c(39,1): error: redefinition 
of 'dwarf_whatform'
[build] dwarf_whatform (Dwarf_Attribute *attr)
[build] ^
[build] C:\work\xemu\elfutils\libdw/libdw.h(1110,1): note: previous definition 
is here
[build] dwarf_whatform (Dwarf_Attribute *attr)
[build] ^
[build] 1 error generated.

Signed-off-by: Yonggang Luo 
---
 libdw/dwarf_whatattr.c | 9 -
 libdw/dwarf_whatform.c | 9 -
 2 files changed, 8 insertions(+), 10 deletions(-)

diff --git a/libdw/dwarf_whatattr.c b/libdw/dwarf_whatattr.c
index d664b021..01d92307 100644
--- a/libdw/dwarf_whatattr.c
+++ b/libdw/dwarf_whatattr.c
@@ -30,13 +30,12 @@
 #ifdef HAVE_CONFIG_H
 # include 
 #endif
-
+#ifndef __OPTIMIZE__
+#define __OPTIMIZE__
+#endif
 #include 
 #include "libdwP.h"
 
 
 unsigned int
-dwarf_whatattr (Dwarf_Attribute *attr)
-{
-  return attr == NULL ? 0 : attr->code;
-}
+dwarf_whatattr (Dwarf_Attribute *attr);
diff --git a/libdw/dwarf_whatform.c b/libdw/dwarf_whatform.c
index dee29a9f..9b3e41e6 100644
--- a/libdw/dwarf_whatform.c
+++ b/libdw/dwarf_whatform.c
@@ -30,13 +30,12 @@
 #ifdef HAVE_CONFIG_H
 # include 
 #endif
-
+#ifndef __OPTIMIZE__
+#define __OPTIMIZE__
+#endif
 #include 
 #include "libdwP.h"
 
 
 unsigned int
-dwarf_whatform (Dwarf_Attribute *attr)
-{
-  return attr == NULL ? 0 : attr->form;
-}
+dwarf_whatform (Dwarf_Attribute *attr);
\ No newline at end of file
-- 
2.36.1.windows.1



[PATCH v2 10/16] libasm: stdio_ext.h are not present on win32

2022-12-17 Thread Yonggang Luo via Elfutils-devel
Signed-off-by: Yonggang Luo 
---
 libasm/asm_begin.c | 7 ++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/libasm/asm_begin.c b/libasm/asm_begin.c
index 9e4dfe43..9b6d974e 100644
--- a/libasm/asm_begin.c
+++ b/libasm/asm_begin.c
@@ -34,10 +34,13 @@
 #include 
 #include 
 #include 
-#include 
 #include 
 #include 
 
+#if !defined(_WIN32)
+#include 
+#endif
+
 #include 
 #include "libasmP.h"
 
@@ -56,8 +59,10 @@ prepare_text_output (AsmCtx_t *result)
  free (result);
  result = NULL;
}
+#if !defined(_WIN32)
   else
__fsetlocking (result->out.file, FSETLOCKING_BYCALLER);
+#endif
 }
 
   return result;
-- 
2.36.1.windows.1



[PATCH v2 11/16] libebl/libdwelf: define ssize_t and pid_t for MSVC within installed header libdwelf.h and libebl.h

2022-12-17 Thread Yonggang Luo via Elfutils-devel
Signed-off-by: Yonggang Luo 
---
 libdwelf/libdwelf.h | 5 +
 libebl/libebl.h | 5 +
 2 files changed, 10 insertions(+)

diff --git a/libdwelf/libdwelf.h b/libdwelf/libdwelf.h
index 263ca60e..167ac0dc 100644
--- a/libdwelf/libdwelf.h
+++ b/libdwelf/libdwelf.h
@@ -31,6 +31,11 @@
 
 #include "libdw.h"
 
+#ifdef _MSC_VER
+#include 
+typedef SSIZE_T ssize_t;
+#endif
+
 #ifdef __cplusplus
 extern "C" {
 #endif
diff --git a/libebl/libebl.h b/libebl/libebl.h
index 731001d3..c568f623 100644
--- a/libebl/libebl.h
+++ b/libebl/libebl.h
@@ -44,6 +44,11 @@
 
 #include "elf-knowledge.h"
 
+#ifdef _MSC_VER
+#include 
+typedef SSIZE_T ssize_t;
+typedef int pid_t;
+#endif
 
 /* Opaque type for the handle.  libasm.h defined the same thing.  */
 #ifndef _LIBASM_H
-- 
2.36.1.windows.1



[PATCH v2 13/16] lib: isatty is not available on windows

2022-12-17 Thread Yonggang Luo via Elfutils-devel
Signed-off-by: Yonggang Luo 
---
 lib/color.c | 5 +
 1 file changed, 5 insertions(+)

diff --git a/lib/color.c b/lib/color.c
index 8063dc26..963a2ec2 100644
--- a/lib/color.c
+++ b/lib/color.c
@@ -117,9 +117,14 @@ parse_opt (int key, char *arg,
if (strcmp (arg, values[i].str) == 0)
  {
color_mode = values[i].mode;
+#ifdef _WIN32
+   if (color_mode == color_auto)
+   color_mode = color_never;
+#else
if (color_mode == color_auto)
  color_mode
= isatty (STDOUT_FILENO) ? color_always : color_never;
+#endif
break;
  }
  if (i == nvalues)
-- 
2.36.1.windows.1



[PATCH v2 08/16] lib: Implement error properly even when not HAVE_ERR_H

2022-12-17 Thread Yonggang Luo via Elfutils-devel
on win32, there is no err.h

Signed-off-by: Yonggang Luo 
---
 lib/error.c  | 16 +++-
 lib/system.h |  4 +---
 2 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/lib/error.c b/lib/error.c
index 5186fc15..d4cbf0ff 100644
--- a/lib/error.c
+++ b/lib/error.c
@@ -28,12 +28,14 @@
 
 #include 
 
-#if !defined(HAVE_ERROR_H) && defined(HAVE_ERR_H)
+#if !defined(HAVE_ERROR_H)
 #include 
 #include 
 #include 
 #include 
+#if defined(HAVE_ERR_H)
 #include 
+#endif
 
 unsigned int error_message_count = 0;
 
@@ -44,6 +46,7 @@ void error(int status, int errnum, const char *format, ...) {
   fflush (stdout);
 
   va_start(argp, format);
+#if defined(HAVE_ERR_H)
   if (status)
 {
   if (errnum)
@@ -64,6 +67,17 @@ void error(int status, int errnum, const char *format, ...) {
   else
 vwarnx (format, argp);
 }
+#else
+  if (errnum)
+{
+  errno = errnum;
+}
+  vfprintf(stderr, format, argp);
+  if (status)
+{
+  _exit(status);
+}
+#endif
   va_end(argp);
 
   fflush (stderr);
diff --git a/lib/system.h b/lib/system.h
index 561d3e03..7f9f2a91 100644
--- a/lib/system.h
+++ b/lib/system.h
@@ -50,11 +50,9 @@
 
 #if defined(HAVE_ERROR_H)
 #include 
-#elif defined(HAVE_ERR_H)
+#else
 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
 
 /* error (EXIT_FAILURE, ...) should be noreturn but on some systems it
-- 
2.36.1.windows.1



[PATCH v2 12/16] libasm/debuginfod: fchmod doesn't present on win32

2022-12-17 Thread Yonggang Luo via Elfutils-devel
Signed-off-by: Yonggang Luo 
---
 debuginfod/debuginfod-client.c | 4 +++-
 libasm/asm_end.c   | 2 ++
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/debuginfod/debuginfod-client.c b/debuginfod/debuginfod-client.c
index 8873fcc8..7a67a440 100644
--- a/debuginfod/debuginfod-client.c
+++ b/debuginfod/debuginfod-client.c
@@ -1708,9 +1708,11 @@ debuginfod_query_server (debuginfod_client *c,
   tvs[0].tv_usec = tvs[1].tv_usec = 0;
   (void) futimes (fd, tvs);  /* best effort */
 
+#if !defined(_WIN32)
   /* PR27571: make cache files casually unwriteable; dirs are already 0700 */
   (void) fchmod(fd, 0400);
-
+#endif
+
   /* rename tmp->real */
   rc = rename (target_cache_tmppath, target_cache_path);
   if (rc < 0)
diff --git a/libasm/asm_end.c b/libasm/asm_end.c
index c06d2366..54540bc1 100644
--- a/libasm/asm_end.c
+++ b/libasm/asm_end.c
@@ -512,12 +512,14 @@ asm_end (AsmCtx_t *ctx)
   if (result != 0)
 return result;
 
+#if !defined(_WIN32)
   /* Make the new file globally readable and user/group-writable.  */
   if (fchmod (ctx->fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH) != 0)
 {
   __libasm_seterrno (ASM_E_CANNOT_CHMOD);
   return -1;
 }
+#endif
 
   /* Rename output file.  */
   if (rename (ctx->tmp_fname, ctx->fname) != 0)
-- 
2.36.1.windows.1



[PATCH v2 09/16] libelf: uid_t, gid_t and mode_t are not comes with msvcrt, so using long/unsigned long instead on win32

2022-12-17 Thread Yonggang Luo via Elfutils-devel
Signed-off-by: Yonggang Luo 
---
 libelf/libelf.h | 6 ++
 1 file changed, 6 insertions(+)

diff --git a/libelf/libelf.h b/libelf/libelf.h
index a139e733..2fa3838b 100644
--- a/libelf/libelf.h
+++ b/libelf/libelf.h
@@ -195,9 +195,15 @@ typedef struct
 {
   char *ar_name;   /* Name of archive member.  */
   time_t ar_date;  /* File date.  */
+#if defined(_WIN32)
+  long ar_uid;
+  long ar_gid;
+  unsigned long ar_mode;
+#else
   uid_t ar_uid;/* User ID.  */
   gid_t ar_gid;/* Group ID.  */
   mode_t ar_mode;  /* File mode.  */
+#endif
   int64_t ar_size; /* File size.  */
   char *ar_rawname;/* Original name of archive member.  */
 } Elf_Arhdr;
-- 
2.36.1.windows.1



[PATCH v2 16/16] lib: Use HAVE_LIBINTL_H to guard #include

2022-12-17 Thread Yonggang Luo via Elfutils-devel
MSVC doesn't have libintl.h, so use macro to guard it.

Signed-off-by: Yonggang Luo 
---
 configure.ac| 2 ++
 lib/eu-config.h | 7 +++
 2 files changed, 9 insertions(+)

diff --git a/configure.ac b/configure.ac
index b84623fe..aea12be3 100644
--- a/configure.ac
+++ b/configure.ac
@@ -441,6 +441,8 @@ AC_CHECK_FUNCS([process_vm_readv mremap])
 AS_IF([test "x$ac_cv_func_mremap" = "xno"],
   [AC_MSG_WARN([elf_update needs mremap to support ELF_C_RDWR_MMAP])])
 
+AC_CHECK_HEADERS([libintl.h])
+
 AC_CHECK_HEADERS([error.h])
 AC_CHECK_HEADERS([err.h])
 
diff --git a/lib/eu-config.h b/lib/eu-config.h
index 78a5c4fe..72b7793e 100644
--- a/lib/eu-config.h
+++ b/lib/eu-config.h
@@ -52,10 +52,17 @@
 # define rwlock_unlock(lock) ((void) (lock))
 #endif /* USE_LOCKS */
 
+#if defined(HAVE_LIBINTL_H)
 #include 
+#endif
+
 /* gettext helper macros.  */
 #define N_(Str) Str
+#if defined(HAVE_LIBINTL_H)
 #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 v2 14/16] Add function sys_get_page_size to replace platform dependent sysconf (_SC_PAGESIZE)

2022-12-17 Thread Yonggang Luo via Elfutils-devel
Signed-off-by: Yonggang Luo 
---
 lib/Makefile.am|  2 +-
 lib/crc32_file.c   |  2 +-
 lib/system.c   | 48 ++
 lib/system.h   |  3 +++
 libdw/dwarf_begin_elf.c|  2 +-
 libdwelf/dwelf_strtab.c|  2 +-
 libdwfl/linux-kernel-modules.c |  2 +-
 libdwfl/linux-proc-maps.c  |  2 +-
 libelf/elf32_updatefile.c  |  2 +-
 src/ar.c   |  2 +-
 src/ranlib.c   |  2 +-
 src/strings.c  |  2 +-
 12 files changed, 61 insertions(+), 10 deletions(-)
 create mode 100644 lib/system.c

diff --git a/lib/Makefile.am b/lib/Makefile.am
index 42ddf5ae..7a50085b 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -35,7 +35,7 @@ noinst_LIBRARIES = libeu.a
 
 libeu_a_SOURCES = xasprintf.c xstrdup.c xstrndup.c xmalloc.c next_prime.c \
  crc32.c crc32_file.c \
- color.c error.c printversion.c
+ color.c error.c printversion.c system.c
 
 noinst_HEADERS = fixedsizehash.h libeu.h system.h dynamicsizehash.h list.h \
 eu-config.h color.h printversion.h bpf.h \
diff --git a/lib/crc32_file.c b/lib/crc32_file.c
index 66833702..45e1cc52 100644
--- a/lib/crc32_file.c
+++ b/lib/crc32_file.c
@@ -52,7 +52,7 @@ crc32_file (int fd, uint32_t *resp)
   void *mapped = mmap (NULL, mapsize, PROT_READ, MAP_PRIVATE, fd, 0);
   if (mapped == MAP_FAILED && errno == ENOMEM)
{
- const size_t pagesize = sysconf (_SC_PAGESIZE);
+ const size_t pagesize = sys_get_page_size();
  mapsize = ((mapsize / 2) + pagesize - 1) & -pagesize;
  while (mapsize >= pagesize
 && (mapped = mmap (NULL, mapsize, PROT_READ, MAP_PRIVATE,
diff --git a/lib/system.c b/lib/system.c
new file mode 100644
index ..bd3831f5
--- /dev/null
+++ b/lib/system.c
@@ -0,0 +1,48 @@
+/* Definitions for system functions.
+   Copyright (C) 2006-2011 Red Hat, Inc.
+   Copyright (C) 2022 Mark J. Wielaard 
+   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"
+#if defined(_WIN32)
+#include 
+#endif
+
+size_t
+sys_get_page_size(void)
+{
+#ifdef _WIN32
+  SYSTEM_INFO info;
+  GetSystemInfo(&info);
+  return info.dwPageSize;
+#else
+  return sysconf (_SC_PAGESIZE);
+#endif
+}
diff --git a/lib/system.h b/lib/system.h
index 7f9f2a91..c84c416c 100644
--- a/lib/system.h
+++ b/lib/system.h
@@ -200,6 +200,9 @@ pread_retry (int fd, void *buf, size_t len, off_t off)
   return recvd;
 }
 
+size_t
+sys_get_page_size(void);
+
 /* The demangler from libstdc++.  */
 extern char *__cxa_demangle (const char *mangled_name, char *output_buffer,
 size_t *length, int *status);
diff --git a/libdw/dwarf_begin_elf.c b/libdw/dwarf_begin_elf.c
index 8fcef335..0f13e2f5 100644
--- a/libdw/dwarf_begin_elf.c
+++ b/libdw/dwarf_begin_elf.c
@@ -502,7 +502,7 @@ dwarf_begin_elf (Elf *elf, Dwarf_Cmd cmd, Elf_Scn *scngrp)
 
 
   /* Default memory allocation size.  */
-  size_t mem_default_size = sysconf (_SC_PAGESIZE) - 4 * sizeof (void *);
+  size_t mem_default_size = sys_get_page_size() - 4 * sizeof (void *);
   assert (sizeof (struct Dwarf) < mem_default_size);
 
   /* Allocate the data structure.  */
diff --git a/libdwelf/dwelf_strtab.c b/libdwelf/dwelf_strtab.c
index c95f9467..293b2d93 100644
--- a/libdwelf/dwelf_strtab.c
+++ b/libdwelf/dwelf_strtab.c
@@ -86,7 +86,7 @@ dwelf_strtab_init (bool nullstr)
 {
   if (ps == 0)
 {
-  ps = sysconf (_SC_PAGESIZE);
+  ps = sys_get_page_size();
   assert (sizeof (struct memoryblock) < ps - MALLOC_OVERHEAD);
 }
 
diff --git a/libdwfl/linux-kernel-modules.c b/libdwfl/linux-kernel-modules.c
index 58c0c417..65a58616 100644
--- a/libdwfl/linux-kernel-modules.c
+++ b/libdwfl/linux-kernel-modules.c
@@ -550,7 +550,7 @@ intuit_kernel_bounds (Dwarf_Addr *start, Dwarf_Addr *end, 
Dwarf_Addr *notes)
*notes = *end;
}
 
-  Dwarf_Addr round_kernel = sysconf (_SC_PAGESIZ

[PATCH v2 15/16] libelf: F_GETFD may not predefined with msvc/mingw, guard the usage of it

2022-12-17 Thread Yonggang Luo via Elfutils-devel
Signed-off-by: Yonggang Luo 
---
 libelf/elf_begin.c | 7 +++
 1 file changed, 7 insertions(+)

diff --git a/libelf/elf_begin.c b/libelf/elf_begin.c
index 6d31882e..3d324694 100644
--- a/libelf/elf_begin.c
+++ b/libelf/elf_begin.c
@@ -1163,12 +1163,19 @@ elf_begin (int fildes, Elf_Cmd cmd, Elf *ref)
   if (ref != NULL)
 /* Make sure the descriptor is not suddenly going away.  */
 rwlock_rdlock (ref->lock);
+#if defined(F_GETFD)
   else if (unlikely (fcntl (fildes, F_GETFD) == -1 && errno == EBADF))
 {
   /* We cannot do anything productive without a file descriptor.  */
   __libelf_seterrno (ELF_E_INVALID_FILE);
   return NULL;
 }
+#else
+  else if (fildes < 0)
+{
+  return NULL;
+}
+#endif
 
   switch (cmd)
 {
-- 
2.36.1.windows.1