From patchwork Tue Sep 20 08:43:06 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yonggang Luo X-Patchwork-Id: 57792 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4715C38582B2 for ; Tue, 20 Sep 2022 08:43:46 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4715C38582B2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1663663426; bh=Rs24UiwdDaCZ1guz21WLvtpkScGmDpLNu3yF2ODDIPE=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Help:List-Subscribe:From:Reply-To:From; b=v08UYfs3zf2aF/04Xm01B9gUM8TbXh8vxbkuGkRB9KKEiVp9RClT8nKd9G6VsUjzg SFaPp9DMshHWHULwjtEM/DRwENSYvFB4w9cYyigzKZGQU380fa6SkVYHPJqL3UnR8v ooX91JTjzQwUF+xUpa8oPvpHm35qsyOH7TBbausQ= X-Original-To: elfutils-devel@sourceware.org Delivered-To: elfutils-devel@sourceware.org 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 Received: by mail-pj1-x102c.google.com with SMTP id s14-20020a17090a6e4e00b0020057c70943so10080044pjm.1 for ; Tue, 20 Sep 2022 01:43:33 -0700 (PDT) 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) To: elfutils-devel@sourceware.org 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 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 X-BeenThere: elfutils-devel@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Elfutils-devel mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-Patchwork-Original-From: Yonggang Luo via Elfutils-devel From: Yonggang Luo Reply-To: Yonggang Luo Errors-To: elfutils-devel-bounces+patchwork=sourceware.org@sourceware.org Sender: "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) & 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 + 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_INVALID_DRIVE, ENOENT}, + {ERROR_CURRENT_DIRECTORY, EACCES}, + {ERROR_NOT_SAME_DEVICE, EXDEV}, + {ERROR_NO_MORE_FILES, ENOENT}, + {ERROR_LOCK_VIOLATION, EACCES}, + {ERROR_SHARING_VIOLATION, EACCES}, + {ERROR_BAD_NETPATH, ENOENT}, + {ERROR_NETWORK_ACCESS_DENIED, EACCES}, + {ERROR_BAD_NET_NAME, ENOENT}, + {ERROR_FILE_EXISTS, EEXIST}, + {ERROR_CANNOT_MAKE, EACCES}, + {ERROR_FAIL_I24, EACCES}, + {ERROR_INVALID_PARAMETER, EINVAL}, + {ERROR_NO_PROC_SLOTS, EAGAIN}, + {ERROR_DRIVE_LOCKED, EACCES}, + {ERROR_BROKEN_PIPE, EPIPE}, + {ERROR_DISK_FULL, ENOSPC}, + {ERROR_INVALID_TARGET_HANDLE, EBADF}, + {ERROR_INVALID_HANDLE, EINVAL}, + {ERROR_WAIT_NO_CHILDREN, ECHILD}, + {ERROR_CHILD_NOT_COMPLETE, ECHILD}, + {ERROR_DIRECT_ACCESS_HANDLE, EBADF}, + {ERROR_NEGATIVE_SEEK, EINVAL}, + {ERROR_SEEK_ON_DEVICE, EACCES}, + {ERROR_DIR_NOT_EMPTY, ENOTEMPTY}, + {ERROR_NOT_LOCKED, EACCES}, + {ERROR_BAD_PATHNAME, ENOENT}, + {ERROR_MAX_THRDS_REACHED, EAGAIN}, + {ERROR_LOCK_FAILED, EACCES}, + {ERROR_ALREADY_EXISTS, EEXIST}, + {ERROR_FILENAME_EXCED_RANGE, ENOENT}, + {ERROR_NESTING_NOT_ALLOWED, EAGAIN}, + {ERROR_NOT_ENOUGH_QUOTA, ENOMEM}, + {ERROR_DELETE_PENDING, ENOENT}}; + +static void +_dosmaperr(unsigned long e) +{ + int i; + + if (e == 0) + { + errno = 0; + return; + } + + for (i = 0; i < sizeof(doserrors) / sizeof(doserrors[0]); i++) + { + if (doserrors[i].winerr == e) + { + int doserr = doserrors[i].doserr; + + errno = doserr; + return; + } + } + + errno = EINVAL; +} + +ssize_t +pread(int fd, void *buf, size_t count, off_t offset) +{ + OVERLAPPED overlapped = {0}; + HANDLE h; + DWORD ret; + + h = (HANDLE)_get_osfhandle(fd); + if (h == INVALID_HANDLE_VALUE) + { + errno = EBADF; + return -1; + } + + overlapped.Offset = offset; + if (!ReadFile(h, buf, count, &ret, &overlapped)) + { + if (GetLastError() == ERROR_HANDLE_EOF) + return 0; + + _dosmaperr(GetLastError()); + return -1; + } + + return ret; +} + +ssize_t +pwrite(int fd, const void *buf, size_t count, off_t offset) +{ + OVERLAPPED overlapped = {0}; + HANDLE h; + DWORD ret; + + h = (HANDLE)_get_osfhandle(fd); + if (h == INVALID_HANDLE_VALUE) + { + errno = EBADF; + return -1; + } + + overlapped.Offset = offset; + if (!WriteFile(h, buf, count, &ret, &overlapped)) + { + _dosmaperr(GetLastError()); + return -1; + } + + return ret; +} 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..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 = 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. */ @@ -1160,12 +1163,14 @@ 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; } +#endif 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) != 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); size = -1; } +#endif if (size != -1 && elf->parent == NULL) elf->maximum_size = 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 +#if defined(_WIN32) +#include "win32/ar.h" +#else #include +#endif #include #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 + 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 . */ + +#ifndef _AR_H +#define _AR_H + +#ifdef __cplusplus +extern "C" { +#endif + +#define ARMAG "!\n" +#define SARMAG 8 +#define ARFMAG "`\n" + +struct ar_hdr { + char ar_name[16]; + char ar_date[12]; + char ar_uid[6], ar_gid[6]; + char ar_mode[8]; + char ar_size[10]; + char ar_fmag[2]; +}; + +#ifdef __cplusplus +} +#endif + +#endif