Repository navigation
C front-end: section attributes do not rename symbols - #9628
Draft
tautschnig wants to merge 1 commit into
Draft
tautschnig wants to merge 1 commit into
tautschnig wants to merge 1 commit into
Conversation
__attribute__((section("name"))) places a function or an object in a
section of the object file without changing its name. The GCC manual
("Common Variable Attributes"): "The section attribute specifies that a
variable (or function) lives in a particular section", and "the linker
requires each object be defined once". ELF symbol names are not
qualified by sections, so declarations without the attribute, in other
translation units or before the attribute in the same one, declare the
same function or object, and GNU ld links them.
Since 48d16ae (2016, no rationale recorded) the C front end named
such a symbol "<section>$$<name>", <section> being the section name up to
its second dot. Nothing in CBMC reads the prefix back. The renaming
broke the linkage of each declaration without the attribute (issue
diffblue#8127): in a goto-ld link of Linux v7.4-rc4's vmlinux.o (arm64
defconfig) 525 functions, among them __do_softirq, were called by their
plain name, which had no body, and 302 objects, among them
__cpu_online_mask, were used by a plain name that was only declared.
Earlier changes worked around consequences of the renaming: 2a45e61
(only the top-level section, as jiffies_64 was redeclared in a
subsection) and e133964 (aliases of renamed functions).
Record the section as comment #section of the symbol's type instead, as
#alignment and #noreturn record other declaration attributes: type
comparisons ignore comments, and goto binaries keep them. When several
declarations name a section, keep the first, as GCC does (it warns;
Clang uses the last one), within a translation unit and when linking
translation units or goto binaries, and when an initializer completes the
type of an array. typeof does not yield the section.
regression/ansi-c/gcc_attributes9 declared, in one file, a function foo,
an object foo in section "other" and an object with asm label "foo" in
section "other2", which only the renaming made acceptable: GCC 15 and
Clang 21 reject the first two ("redeclared as different kind of
symbol"), and the third is a syntax error for both (the asm label must
come before the attributes; with the label first, GCC's assembler
rejects the second definition of foo). Remove these declarations. Its declaration of txt_heap_base and txt_heap_size
put txt_heap_size in two sections, which GCC 15 rejects ("section of
'txt_heap_size' conflicts with previous declaration"); name the same
section in both attributes, as `__initdata a, __initdata b` does in the
kernel.
Tests, all of which fail before this change:
- cbmc/section_attribute_linkage: diffblue#8127's example (from diffblue#8870), and
metadata.desc: the linked symbol table keeps the section of the
definition although the file linked first declares p without it;
- cbmc/section_attribute_linkage_functions: the kernel's patterns,
objects and functions defined with section attributes in a file linked
before and in one linked after the file that uses them by declarations
without the attribute, uses before the definitions in the defining
file, static functions and objects with forward declarations, and a
tentative definition before the initialized definition with the
attribute (kernel/sched/fair.c's fair_sched_class);
- linking-goto-binaries/section_attribute: separate compilation, goto
binaries linked in both orders;
- goto-cc-cbmc/section_attribute_metadata: the sections recorded in a
goto binary's symbol table, also of an array whose size the initializer
determines (as libcap's `const char __execable_dl_loader[]
__attribute((section(".interp"))) = ...`).
Fixes: diffblue#8127
Co-authored-by: Kiro <kiro-agent@users.noreply.github.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
attribute((section("name"))) places a function or an object in a section of the object file without changing its name. The GCC manual ("Common Variable Attributes"): "The section attribute specifies that a variable (or function) lives in a particular section", and "the linker requires each object be defined once". ELF symbol names are not qualified by sections, so declarations without the attribute, in other translation units or before the attribute in the same one, declare the same function or object, and GNU ld links them.
Since 48d16ae (2016, no rationale recorded) the C front end named such a symbol "
Record the section as comment #section of the symbol's type instead, as #alignment and #noreturn record other declaration attributes: type comparisons ignore comments, and goto binaries keep them. When several declarations name a section, keep the first, as GCC does (it warns; Clang uses the last one), within a translation unit and when linking translation units or goto binaries, and when an initializer completes the type of an array. typeof does not yield the section.
regression/ansi-c/gcc_attributes9 declared, in one file, a function foo, an object foo in section "other" and an object with asm label "foo" in section "other2", which only the renaming made acceptable: GCC 15 and Clang 21 reject the first two ("redeclared as different kind of symbol"), and the third is a syntax error for both (the asm label must come before the attributes; with the label first, GCC's assembler rejects the second definition of foo). Remove these declarations. Its declaration of txt_heap_base and txt_heap_size put txt_heap_size in two sections, which GCC 15 rejects ("section of 'txt_heap_size' conflicts with previous declaration"); name the same section in both attributes, as
__initdata a, __initdata bdoes in the kernel.Tests, all of which fail before this change:
const char __execable_dl_loader[] __attribute((section(".interp"))) = ...).Fixes: #8127