Skip to content

[LLD] lld linker script does not support library: or :file file descriptor patterns #119584

@smithp35

Description

@smithp35

This was found by an internal project while trying to migrate from a GNU toolchain to a LLVM based toolchain.

The GNU ld manual https://sourceware.org/binutils/docs/ld/Input-Section-Basics.html has the following section:

You can also specify files within archives by writing a pattern matching the archive, a colon, then the pattern matching the file, with no whitespace around the colon.

‘archive:file’
matches file within archive

‘archive:’
matches the whole archive

‘:file’
matches file but not one in an archive

LLD will support archive:file but it does not support archive: or :file.

For example:
app.s

        .text
        .global _start
        .type _start, %function
        .global foo
_start: call foo

lib.s

        .text
        .global foo
        .type foo, %function
foo:    nop

app.ld

SECTIONS {
  lib : { lib.a:(*.text) }      /* Expect lib.a(lib.o):(.text) */
  app : { :app.o(*.text) } /* Expect app.o(.text) */
  all : { *(.text) } /* Expect no matches */
}

clang --target=x86_64 -c app.s lib.s
llvm-ar -rcs lib.a lib.o
ld.ldd app.o lib.a --script=app.ld --print-map

             VMA              LMA     Size Align Out     In      Symbol
               0                0        9     4 all
               0                0        5     4         app.o:(.text)
               0                0        0     1                 _start
               8                8        1     4         lib.a(lib.o):(.text)
               8                8        0     1                 foo

We can see the .text has been matched by all. GNU ld matches as we would expect:
ld.bfd app.o lib.a --script=app.ld --print-map

lib             0x0000000000000000        0x1
 lib.a:(*.text)
 .text          0x0000000000000000        0x1 lib.a(lib.o)
                0x0000000000000000                foo

app             0x0000000000000004        0x5
 :app.o(*.text)
 .text          0x0000000000000004        0x5 app.o
                0x0000000000000004                _start

There is a workaround for the library pattern by using library:* but I can't think of one for the :file

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions