From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pj1-x102c.google.com (mail-pj1-x102c.google.com [IPv6:2607:f8b0:4864:20::102c]) by sourceware.org (Postfix) with ESMTPS id A89AF38582B8 for ; Tue, 20 Sep 2022 08:43:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A89AF38582B8 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pj1-x102c.google.com with SMTP id s14-20020a17090a6e4e00b0020057c70943so10080044pjm.1 for ; Tue, 20 Sep 2022 01:43:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=Rs24UiwdDaCZ1guz21WLvtpkScGmDpLNu3yF2ODDIPE=; b=neTdwsMMeF5i3Rig1sqTptdEOmwcZP8v0nOhiulr1sniH8N9jKSlAFv2C/6YN9jWBR 0UofoHZQoaF/GKf2VGF5b5nkPN7NwPPi6iAVQXvUuNFVBMrfB8qo8qm9ansTMdQLxd/C CBIk8wRc7Qy5RjI35XNUWdm/Gemp45N2V4w+fSPMHQNsLOUT6ZV3bEPqgH0mP7QwsDUG sfl8F2hrt2twNiDRL2CsnZn23z+2ZJB/9i/tXbS/2yX4jTY2ccHpQ9kuGYrXO5HOGjIr tOYs70MzqcnNsVsuu3xM21wJWf/RoZ0eZDm8exfI5l3H+ubdKy3xw10FPRd3WRroj1y/ dCvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=Rs24UiwdDaCZ1guz21WLvtpkScGmDpLNu3yF2ODDIPE=; b=DY6hSUvMAekSsUxCQKgwSSOOI1R/mVwok5lvY0Jw+SEvith7HNw21R4EJooZ+6IFsc zUhSH40oDztcsLIwkEDfQw9ghBN6JQJGh/IjD4YK8CH130u68PCURvx2iBf0z5rD81Yw SgLXXr/S6NYKOi/3gMobL42p02v+B+j/sLz0qaaMBHUF/6ctSWyt/qFXYl538Ga3Vrr7 DI1Dq5VMaO+hBZP1yk8GeWJXLG+dXUwDHaB1Opv7HqkTTzl8oVytKa8npcNcQYLUz36j ROsx1r3JJMvwbP6mAoWWf0gQOrGS8IzvCbwrgRDQ9a9Wrpud5qQM6wXgyD84tKoVpLME 3vzA== X-Gm-Message-State: ACrzQf1mpUiIkMhnbudQzwKezSNvOM8Gg/ajFsKhZHjIzoQmBUP/0aEY B6BqyuT5W9296CAp5rX1lraP/fHQAMA= X-Google-Smtp-Source: AMsMyM6WFVvTsXJvGkJHGNIF0tYwAnIamQWuAErOtsNRMgRGB7Oa4evWuY0yuTm9KNORix7WwhoZoQ== X-Received: by 2002:a17:90b:3ec7:b0:202:b984:8436 with SMTP id rm7-20020a17090b3ec700b00202b9848436mr2775433pjb.4.1663663412238; Tue, 20 Sep 2022 01:43:32 -0700 (PDT) Received: from localhost.localdomain ([103.94.185.75]) by smtp.googlemail.com with ESMTPSA id so16-20020a17090b1f9000b002009db534d1sm873372pjb.24.2022.09.20.01.43.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Sep 2022 01:43:31 -0700 (PDT) From: Yonggang Luo To: elfutils-devel@sourceware.org Cc: Yonggang Luo Subject: [PATCH 6/7] Fixes building with msvc/clang mingw/gcc Date: Tue, 20 Sep 2022 16:43:06 +0800 Message-Id: <20220920084307.1696-7-luoyonggang@gmail.com> X-Mailer: git-send-email 2.36.1.windows.1 In-Reply-To: <20220920084307.1696-1-luoyonggang@gmail.com> References: <20220920084307.1696-1-luoyonggang@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-10.6 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,KAM_SHORT,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: 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 =20 /* 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 =20 #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 =20 +#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) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | + (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24) + ); +} + +static inline uint64_t bswap_64(uint64_t x) { + return ( + (((x) & 0xff00000000000000ull) >> 56) | + (((x) & 0x00ff000000000000ull) >> 40) | + (((x) & 0x0000ff0000000000ull) >> 24) | + (((x) & 0x000000ff00000000ull) >> 8) | + (((x) & 0x00000000ff000000ull) << 8) | + (((x) & 0x0000000000ff0000ull) << 24) | + (((x) & 0x000000000000ff00ull) << 40) | + (((x) & 0x00000000000000ffull) << 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 00000000..d9941cc5 --- /dev/null +++ b/lib/system_win32.c @@ -0,0 +1,162 @@ +/* Copyright (C) 2022 Yonggang Luo=0D + This file is part of elfutils.=0D +=0D + This file is free software; you can redistribute it and/or modify=0D + it under the terms of either=0D +=0D + * the GNU Lesser General Public License as published by the Free=0D + Software Foundation; either version 3 of the License, or (at=0D + your option) any later version=0D +=0D + or=0D +=0D + * the GNU General Public License as published by the Free=0D + Software Foundation; either version 2 of the License, or (at=0D + your option) any later version=0D +=0D + or both in parallel, as here.=0D +=0D + elfutils is distributed in the hope that it will be useful, but=0D + WITHOUT ANY WARRANTY; without even the implied warranty of=0D + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU=0D + General Public License for more details.=0D +=0D + You should have received copies of the GNU General Public License and=0D + the GNU Lesser General Public License along with this program. If=0D + not, see . */=0D +=0D +#include =0D +#include "system.h"=0D +=0D +#include =0D +=0D +static const struct=0D +{=0D + DWORD winerr;=0D + int doserr;=0D +} doserrors[] =3D=0D +=0D + {=0D + {ERROR_INVALID_FUNCTION, EINVAL},=0D + {ERROR_FILE_NOT_FOUND, ENOENT},=0D + {ERROR_PATH_NOT_FOUND, ENOENT},=0D + {ERROR_TOO_MANY_OPEN_FILES, EMFILE},=0D + {ERROR_ACCESS_DENIED, EACCES},=0D + {ERROR_INVALID_HANDLE, EBADF},=0D + {ERROR_ARENA_TRASHED, ENOMEM},=0D + {ERROR_NOT_ENOUGH_MEMORY, ENOMEM},=0D + {ERROR_INVALID_BLOCK, ENOMEM},=0D + {ERROR_BAD_ENVIRONMENT, E2BIG},=0D + {ERROR_BAD_FORMAT, ENOEXEC},=0D + {ERROR_INVALID_ACCESS, EINVAL},=0D + {ERROR_INVALID_DATA, EINVAL},=0D + {ERROR_INVALID_DRIVE, ENOENT},=0D + {ERROR_CURRENT_DIRECTORY, EACCES},=0D + {ERROR_NOT_SAME_DEVICE, EXDEV},=0D + {ERROR_NO_MORE_FILES, ENOENT},=0D + {ERROR_LOCK_VIOLATION, EACCES},=0D + {ERROR_SHARING_VIOLATION, EACCES},=0D + {ERROR_BAD_NETPATH, ENOENT},=0D + {ERROR_NETWORK_ACCESS_DENIED, EACCES},=0D + {ERROR_BAD_NET_NAME, ENOENT},=0D + {ERROR_FILE_EXISTS, EEXIST},=0D + {ERROR_CANNOT_MAKE, EACCES},=0D + {ERROR_FAIL_I24, EACCES},=0D + {ERROR_INVALID_PARAMETER, EINVAL},=0D + {ERROR_NO_PROC_SLOTS, EAGAIN},=0D + {ERROR_DRIVE_LOCKED, EACCES},=0D + {ERROR_BROKEN_PIPE, EPIPE},=0D + {ERROR_DISK_FULL, ENOSPC},=0D + {ERROR_INVALID_TARGET_HANDLE, EBADF},=0D + {ERROR_INVALID_HANDLE, EINVAL},=0D + {ERROR_WAIT_NO_CHILDREN, ECHILD},=0D + {ERROR_CHILD_NOT_COMPLETE, ECHILD},=0D + {ERROR_DIRECT_ACCESS_HANDLE, EBADF},=0D + {ERROR_NEGATIVE_SEEK, EINVAL},=0D + {ERROR_SEEK_ON_DEVICE, EACCES},=0D + {ERROR_DIR_NOT_EMPTY, ENOTEMPTY},=0D + {ERROR_NOT_LOCKED, EACCES},=0D + {ERROR_BAD_PATHNAME, ENOENT},=0D + {ERROR_MAX_THRDS_REACHED, EAGAIN},=0D + {ERROR_LOCK_FAILED, EACCES},=0D + {ERROR_ALREADY_EXISTS, EEXIST},=0D + {ERROR_FILENAME_EXCED_RANGE, ENOENT},=0D + {ERROR_NESTING_NOT_ALLOWED, EAGAIN},=0D + {ERROR_NOT_ENOUGH_QUOTA, ENOMEM},=0D + {ERROR_DELETE_PENDING, ENOENT}};=0D +=0D +static void=0D +_dosmaperr(unsigned long e)=0D +{=0D + int i;=0D +=0D + if (e =3D=3D 0)=0D + {=0D + errno =3D 0;=0D + return;=0D + }=0D +=0D + for (i =3D 0; i < sizeof(doserrors) / sizeof(doserrors[0]); i++)=0D + {=0D + if (doserrors[i].winerr =3D=3D e)=0D + {=0D + int doserr =3D doserrors[i].doserr;=0D +=0D + errno =3D doserr;=0D + return;=0D + }=0D + }=0D +=0D + errno =3D EINVAL;=0D +}=0D +=0D +ssize_t=0D +pread(int fd, void *buf, size_t count, off_t offset)=0D +{=0D + OVERLAPPED overlapped =3D {0};=0D + HANDLE h;=0D + DWORD ret;=0D +=0D + h =3D (HANDLE)_get_osfhandle(fd);=0D + if (h =3D=3D INVALID_HANDLE_VALUE)=0D + {=0D + errno =3D EBADF;=0D + return -1;=0D + }=0D +=0D + overlapped.Offset =3D offset;=0D + if (!ReadFile(h, buf, count, &ret, &overlapped))=0D + {=0D + if (GetLastError() =3D=3D ERROR_HANDLE_EOF)=0D + return 0;=0D +=0D + _dosmaperr(GetLastError());=0D + return -1;=0D + }=0D +=0D + return ret;=0D +}=0D +=0D +ssize_t=0D +pwrite(int fd, const void *buf, size_t count, off_t offset)=0D +{=0D + OVERLAPPED overlapped =3D {0};=0D + HANDLE h;=0D + DWORD ret;=0D +=0D + h =3D (HANDLE)_get_osfhandle(fd);=0D + if (h =3D=3D INVALID_HANDLE_VALUE)=0D + {=0D + errno =3D EBADF;=0D + return -1;=0D + }=0D +=0D + overlapped.Offset =3D offset;=0D + if (!WriteFile(h, buf, count, &ret, &overlapped))=0D + {=0D + _dosmaperr(GetLastError());=0D + return -1;=0D + }=0D +=0D + return ret;=0D +}=0D 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); } =20 - +#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) =20 return 0; } +#endif =20 =20 /* 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..d867cd6f 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 =3D NULL; int use_mmap =3D (cmd =3D=3D ELF_C_READ_MMAP || cmd =3D=3D ELF_C_RDWR_MM= AP || cmd =3D=3D ELF_C_WRITE_MMAP || cmd =3D=3D ELF_C_READ_MMAP_PRIVATE); +#endif =20 if (parent =3D=3D NULL) { @@ -669,7 +671,7 @@ read_file (int fildes, int64_t offset, size_t maxsize, /* The parent is already loaded. Use it. */ assert (maxsize !=3D ~((size_t) 0)); } - +#if HAVE_DECL_MMAP if (use_mmap) { if (parent =3D=3D NULL) @@ -713,6 +715,7 @@ read_file (int fildes, int64_t offset, size_t maxsize, =20 return result; } +#endif =20 /* 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. */ @@ -1160,12 +1163,14 @@ elf_begin (int fildes, Elf_Cmd cmd, Elf *ref) if (ref !=3D 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) =3D=3D -1 && errno =3D=3D EBA= DF)) { /* We cannot do anything productive without a file descriptor. */ __libelf_seterrno (ELF_E_INVALID_FILE); return NULL; } +#endif =20 switch (cmd) { 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) !=3D 0) free (elf->map_address); +#if HAVE_DECL_MMAP else if ((elf->flags & ELF_F_MMAPPED) !=3D 0) munmap (elf->map_address, elf->maximum_size); +#endif } =20 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 =3D=3D NULL && elf->cmd =3D=3D ELF_C_WRITE_MMAP) { @@ -125,6 +125,7 @@ write_file (Elf *elf, int64_t size, int change_bo, size= _t shnum) size =3D -1; } else +#endif { /* The file is not mmaped. */ if ((class =3D=3D ELFCLASS32 @@ -145,6 +146,7 @@ write_file (Elf *elf, int64_t size, int change_bo, size= _t shnum) size =3D -1; } =20 +#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); size =3D -1; } +#endif =20 if (size !=3D -1 && elf->parent =3D=3D NULL) elf->maximum_size =3D size; 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; diff --git a/libelf/libelfP.h b/libelf/libelfP.h index d88a613c..14b60bce 100644 --- a/libelf/libelfP.h +++ b/libelf/libelfP.h @@ -30,7 +30,11 @@ #ifndef _LIBELFP_H #define _LIBELFP_H 1 =20 +#if defined(_WIN32) +#include "win32/ar.h" +#else #include +#endif #include =20 #include diff --git a/libelf/win32/ar.h b/libelf/win32/ar.h new file mode 100644 index 00000000..d67fab52 --- /dev/null +++ b/libelf/win32/ar.h @@ -0,0 +1,52 @@ +/* Copyright (C) 2022 Yonggang Luo=0D + This file is part of elfutils.=0D +=0D + This file is free software; you can redistribute it and/or modify=0D + it under the terms of either=0D +=0D + * the GNU Lesser General Public License as published by the Free=0D + Software Foundation; either version 3 of the License, or (at=0D + your option) any later version=0D +=0D + or=0D +=0D + * the GNU General Public License as published by the Free=0D + Software Foundation; either version 2 of the License, or (at=0D + your option) any later version=0D +=0D + or both in parallel, as here.=0D +=0D + elfutils is distributed in the hope that it will be useful, but=0D + WITHOUT ANY WARRANTY; without even the implied warranty of=0D + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU=0D + General Public License for more details.=0D +=0D + You should have received copies of the GNU General Public License and=0D + the GNU Lesser General Public License along with this program. If=0D + not, see . */=0D +=0D +#ifndef _AR_H=0D +#define _AR_H=0D +=0D +#ifdef __cplusplus=0D +extern "C" {=0D +#endif=0D +=0D +#define ARMAG "!\n"=0D +#define SARMAG 8=0D +#define ARFMAG "`\n"=0D +=0D +struct ar_hdr {=0D + char ar_name[16];=0D + char ar_date[12];=0D + char ar_uid[6], ar_gid[6];=0D + char ar_mode[8];=0D + char ar_size[10];=0D + char ar_fmag[2];=0D +};=0D +=0D +#ifdef __cplusplus=0D +}=0D +#endif=0D +=0D +#endif=0D --=20 2.36.1.windows.1