Skip to content

C front-end: section attributes do not rename symbols - #9628

Draft
tautschnig wants to merge 1 commit into
diffblue:developfrom
tautschnig:kiro/section-attribute-no-renaming
Draft

tautschnig wants to merge 1 commit into
diffblue:developfrom
tautschnig:kiro/section-attribute-no-renaming

Conversation

@tautschnig

Copy link
Copy Markdown
Collaborator

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 "

$$",
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 #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: Wrong linkage of global variables annotated with __attribute__((section)) #8127's example (from Fix incorrect linkage of global variables with section attributes #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: #8127

  • Each commit message has a non-empty body, explaining why the change was made.
  • n/a Methods or procedures I have added are documented, following the guidelines provided in CODING_STANDARD.md.
  • n/a The feature or user visible behaviour I have added or modified has been documented in the User Guide in doc/cprover-manual/
  • Regression or unit tests are included, or existing tests cover the modified code (in this case I have detailed which ones those are in the commit message).
  • n/a My commit message includes data points confirming performance improvements (if claimed).
  • My PR is restricted to a single feature or bugfix.
  • n/a White-space or formatting changes outside the feature-related changed lines are in commits of their own.

__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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wrong linkage of global variables annotated with __attribute__((section))

1 participant