From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lj1-x234.google.com (mail-lj1-x234.google.com [IPv6:2a00:1450:4864:20::234]) by sourceware.org (Postfix) with ESMTPS id 674703858C50 for ; Thu, 28 Jul 2022 13:37:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 674703858C50 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=martin.st Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=martin.st Received: by mail-lj1-x234.google.com with SMTP id w18so2012953lje.1 for ; Thu, 28 Jul 2022 06:37:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=martin-st.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=6jRC98FttcweLD6XccSkEo1EMp4feSu90+FxGWNECbQ=; b=Gr6YSJb8wNivrz1RG59shoM3nTqF7pfrcOAxB+KOoxki3sHigLjhpjSABEoX5BrPDV QxpxNJbnKSkwtpL7YUALWOoToLknQst9vowQWZFv9ncQJ1ur6IzQpuJ/bnStMXwIvtOo 5MEXqMVDyxIH/HB1VwEz5p5BomeH/i+D+/ku5TqstVZcsB48XdTByZDETlk24TgUQZeF 1vmlSS9gyI19i7x864pDWGY/QHSMlJGJHeRNdgVROnCVqvX1E3xU7YE2QFFBRnOl/EJK yaNF3gJnyuptkZ/4Wb8+2oMCSY6Wc7ugJ1F0MEmfQXNMW1Kz0bksTUMo0IWU1LsXzhtJ b//Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=6jRC98FttcweLD6XccSkEo1EMp4feSu90+FxGWNECbQ=; b=e+DZZIjE35aQuD/ye0Ft27O3tsjmfackRvES0fnaDBJwTv60NcIWad7NQycvNP/p4k sgwvxwaJsGxrV72OduVgK8h85u4kWDhbInbV/HsDYeEmJ7WjWe/arPSJJoUlbS6SdZ82 nHLY8YHevTVSmFgey+2Bqu7apYjlqDK6WsWS3V2Lpkb3XCJvxzANEKxZegfTE3mw6Yhq CQBLjunnFzXz5FpuHQSsY7olbgW0vyz1FWFmW5vhWy0SxcCDCjA5J5ASg82CHG9YdxE8 /R72fJYT1JwLufGNysR0XKVqd2S5euG8EHP6VBaKwM+ONkyDYisc/chBn1++WYrprHV/ n5+w== X-Gm-Message-State: AJIora+h7zJKllxUMtD8dd3mkJSPe1OL/zoM33/6KSglBeDtT9b2/6nO KGQb4DM/YLqj9lbg2FM+fuKfdI1+N0RYi//B X-Google-Smtp-Source: AGRyM1vHfKhs+74Mv7z+TxNJFXz7stoeCrDg7+dc2dkRsBwpOTQWTmUSMPiQwMNWDQhuV7FXR9fr3w== X-Received: by 2002:a2e:6819:0:b0:25e:9d0:728f with SMTP id c25-20020a2e6819000000b0025e09d0728fmr6078759lja.223.1659015459748; Thu, 28 Jul 2022 06:37:39 -0700 (PDT) Received: from localhost.localdomain (dsl-tkubng21-58c01c-243.dhcp.inet.fi. [88.192.28.243]) by smtp.gmail.com with ESMTPSA id y21-20020a05651c021500b0025d6ecbc897sm155167ljn.46.2022.07.28.06.37.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Jul 2022 06:37:38 -0700 (PDT) From: Martin Storsjo To: binutils@sourceware.org Subject: [PATCH v3 1/2] ld: Add support for a new option, -exclude-symbols, in COFF object file directives Date: Thu, 28 Jul 2022 16:37:36 +0300 Message-Id: <20220728133737.4049560-1-martin@martin.st> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-13.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, JMQ_SPF_NEUTRAL, 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: binutils@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 28 Jul 2022 13:37:46 -0000 This maps to the same as ld's --exclude-symbols command line option, but allowing specifying the option via directives embedded in the object files instead of passed manually on the command line. --- v2: Fixed parsing multiple comma separated symbols. While pe_dll_add_excludes does tokenize the string, the symbol tokens in deffilep.y don't include commas, so we need to handle that at that level. v3: Added an x86_64 testcase too, changed i*86 into i?86 in the testcases. --- ld/deffile.h | 6 +++ ld/deffilep.y | 50 ++++++++++++++++++- ld/pe-dll.c | 10 ++++ .../ld-pe/exclude-symbols-embedded-i386.d | 10 ++++ .../ld-pe/exclude-symbols-embedded-i386.s | 15 ++++++ .../ld-pe/exclude-symbols-embedded-x86_64.d | 10 ++++ .../ld-pe/exclude-symbols-embedded-x86_64.s | 15 ++++++ ld/testsuite/ld-pe/pe.exp | 3 ++ 8 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 ld/testsuite/ld-pe/exclude-symbols-embedded-i386.d create mode 100644 ld/testsuite/ld-pe/exclude-symbols-embedded-i386.s create mode 100644 ld/testsuite/ld-pe/exclude-symbols-embedded-x86_64.d create mode 100644 ld/testsuite/ld-pe/exclude-symbols-embedded-x86_64.s diff --git a/ld/deffile.h b/ld/deffile.h index d68fa8c6ba1..306ae3a6f75 100644 --- a/ld/deffile.h +++ b/ld/deffile.h @@ -61,6 +61,11 @@ typedef struct def_file_aligncomm { unsigned int alignment; /* log-2 alignment. */ } def_file_aligncomm; +typedef struct def_file_exclude_symbol { + struct def_file_exclude_symbol *next; /* Chain pointer. */ + char *symbol_name; /* Name of excluded symbol. */ +} def_file_exclude_symbol; + typedef struct def_file { /* From the NAME or LIBRARY command. */ char *name; @@ -94,6 +99,7 @@ typedef struct def_file { /* Only expected from .drectve sections, not .DEF files. */ def_file_aligncomm *aligncomms; + def_file_exclude_symbol *exclude_symbols; } def_file; diff --git a/ld/deffilep.y b/ld/deffilep.y index 3f610ddbe14..126f33a1352 100644 --- a/ld/deffilep.y +++ b/ld/deffilep.y @@ -101,6 +101,7 @@ static void def_stacksize (int, int); static void def_version (int, int); static void def_directive (char *); static void def_aligncomm (char *str, int align); +static void def_exclude_symbols (char *str); static int def_parse (void); static void def_error (const char *); static int def_lex (void); @@ -121,7 +122,7 @@ static const char *lex_parse_string_end = 0; %token NAME LIBRARY DESCRIPTION STACKSIZE_K HEAPSIZE CODE DATAU DATAL %token SECTIONS EXPORTS IMPORTS VERSIONK BASE CONSTANTU CONSTANTL -%token PRIVATEU PRIVATEL ALIGNCOMM +%token PRIVATEU PRIVATEL ALIGNCOMM EXCLUDE_SYMBOLS %token READ WRITE EXECUTE SHARED_K NONAMEU NONAMEL DIRECTIVE EQUAL %token ID %token DIGITS @@ -131,7 +132,7 @@ static const char *lex_parse_string_end = 0; %type opt_ordinal %type attr attr_list opt_number exp_opt_list exp_opt %type opt_name opt_name2 opt_equal_name anylang_id opt_id -%type opt_equalequal_name +%type opt_equalequal_name symbol_list %type keyword_as_name %% @@ -155,6 +156,7 @@ command: | VERSIONK NUMBER '.' NUMBER { def_version ($2, $4);} | DIRECTIVE ID { def_directive ($2);} | ALIGNCOMM anylang_id ',' NUMBER { def_aligncomm ($2, $4);} + | EXCLUDE_SYMBOLS symbol_list ; @@ -333,6 +335,11 @@ anylang_id: ID { $$ = $1; } } ; +symbol_list: + anylang_id { def_exclude_symbols ($1); } + | symbol_list ',' anylang_id { def_exclude_symbols ($3); } + ; + opt_digits: DIGITS { $$ = $1; } | { $$ = ""; } ; @@ -488,6 +495,15 @@ def_file_free (def_file *fdef) free (c); } + while (fdef->exclude_symbols) + { + def_file_exclude_symbol *e = fdef->exclude_symbols; + + fdef->exclude_symbols = fdef->exclude_symbols->next; + free (e->symbol_name); + free (e); + } + free (fdef); } @@ -946,6 +962,7 @@ diropts[] = { "-attr", SECTIONS }, { "-export", EXPORTS }, { "-aligncomm", ALIGNCOMM }, + { "-exclude-symbols", EXCLUDE_SYMBOLS }, { 0, 0 } }; @@ -1261,6 +1278,35 @@ def_aligncomm (char *str, int align) } } +static void +def_exclude_symbols (char *str) +{ + def_file_exclude_symbol *c, *p; + + p = NULL; + c = def->exclude_symbols; + while (c != NULL) + { + int e = strcmp (c->symbol_name, str); + if (!e) + return; + c = (p = c)->next; + } + + c = xmalloc (sizeof (def_file_exclude_symbol)); + c->symbol_name = xstrdup (str); + if (!p) + { + c->next = def->exclude_symbols; + def->exclude_symbols = c; + } + else + { + c->next = p->next; + p->next = c; + } +} + static void def_error (const char *err) { diff --git a/ld/pe-dll.c b/ld/pe-dll.c index db2c75da9b5..33be53d6164 100644 --- a/ld/pe-dll.c +++ b/ld/pe-dll.c @@ -718,6 +718,16 @@ process_def_file_and_drectve (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_link_info * } } + if (pe_def_file->exclude_symbols) + { + def_file_exclude_symbol *ac = pe_def_file->exclude_symbols; + while (ac) + { + pe_dll_add_excludes (ac->symbol_name, EXCLUDESYMS); + ac = ac->next; + } + } + /* If we are building an executable and there is nothing to export, we do not build an export table at all. */ if (bfd_link_executable (info) && pe_def_file->num_exports == 0 diff --git a/ld/testsuite/ld-pe/exclude-symbols-embedded-i386.d b/ld/testsuite/ld-pe/exclude-symbols-embedded-i386.d new file mode 100644 index 00000000000..663e6b4dc89 --- /dev/null +++ b/ld/testsuite/ld-pe/exclude-symbols-embedded-i386.d @@ -0,0 +1,10 @@ +#source: exclude-symbols-embedded-i386.s +#target: i?86-*-cygwin* i?86-*-pe i?86-*-mingw* +#ld: -shared +#objdump: -p + +#... +.*\[[ ]*0\] sym1 +.*\[[ ]*1\] sym3 +.*\[[ ]*2\] sym5 +#pass diff --git a/ld/testsuite/ld-pe/exclude-symbols-embedded-i386.s b/ld/testsuite/ld-pe/exclude-symbols-embedded-i386.s new file mode 100644 index 00000000000..d9da979f9f7 --- /dev/null +++ b/ld/testsuite/ld-pe/exclude-symbols-embedded-i386.s @@ -0,0 +1,15 @@ +.global _sym1 +.global _sym2 +.global _sym3 +.global _sym4 +.global _sym5 +_sym1: +_sym2: +_sym3: +_sym4: +_sym5: + ret + +.section .drectve,"yn" +.ascii " -exclude-symbols:sym2,unknownsym" +.ascii " -exclude-symbols:unknownsym,sym4" diff --git a/ld/testsuite/ld-pe/exclude-symbols-embedded-x86_64.d b/ld/testsuite/ld-pe/exclude-symbols-embedded-x86_64.d new file mode 100644 index 00000000000..102569515f7 --- /dev/null +++ b/ld/testsuite/ld-pe/exclude-symbols-embedded-x86_64.d @@ -0,0 +1,10 @@ +#source: exclude-symbols-embedded-x86_64.s +#target: x86_64-*-cygwin* x86_64-*-pe x86_64-*-mingw* +#ld: -shared +#objdump: -p + +#... +.*\[[ ]*0\] sym1 +.*\[[ ]*1\] sym3 +.*\[[ ]*2\] sym5 +#pass diff --git a/ld/testsuite/ld-pe/exclude-symbols-embedded-x86_64.s b/ld/testsuite/ld-pe/exclude-symbols-embedded-x86_64.s new file mode 100644 index 00000000000..2fb0d2adb74 --- /dev/null +++ b/ld/testsuite/ld-pe/exclude-symbols-embedded-x86_64.s @@ -0,0 +1,15 @@ +.global sym1 +.global sym2 +.global sym3 +.global sym4 +.global sym5 +sym1: +sym2: +sym3: +sym4: +sym5: + ret + +.section .drectve,"yn" +.ascii " -exclude-symbols:sym2,unknownsym" +.ascii " -exclude-symbols:unknownsym,sym4" diff --git a/ld/testsuite/ld-pe/pe.exp b/ld/testsuite/ld-pe/pe.exp index 413b316f152..b640b0891f2 100644 --- a/ld/testsuite/ld-pe/pe.exp +++ b/ld/testsuite/ld-pe/pe.exp @@ -124,3 +124,6 @@ set foreign_sym_test { # MCore rearranges symbol order. setup_xfail mcore-*-pe run_ld_link_tests $foreign_sym_test + +run_dump_test "exclude-symbols-embedded-i386" +run_dump_test "exclude-symbols-embedded-x86_64" -- 2.25.1