This patch is to use ELFT instead of Is64Bits to template OutputSection
and its subclasses. This increases code size slightly because it creates
two identical functions for some classes, but that's only 20 KB out of
33 MB, so it's negligible.
This is as per discussion with Rafael. He's not fan of the idea but OK
with this. We'll revisit later to this topic.
llvm-svn: 250466
String table is added to end of the file so that all the other sections
are finalized before string table. But we can just add section names to
the string table before calling finalize() on any section instead.
llvm-svn: 250463
If a section name is valid as a C identifier (which is rare because of
the leading '.'), linkers are expected to define __start_<secname> and
__stop_<secname> symbols. They are at beginning and end of the section,
respectively. This is not requested by the ELF standard, but GNU ld and
gold provide this feature.
llvm-svn: 250432
Unfortunately, the check was not as dead as I had thought, and adjusting the
starting VA again exposed the problem. We end up trying to relocate the bl
(using a 24-bit relative offset) to a symbol address of zero, and in general,
that does not fit.
Thus, reverting for now, and adding a test case.
llvm-svn: 250423
When a relocation points to a SHF_MERGE section, the addend has special meaning.
It should be used to find what in the section the relocation points to. It
should not be added to the output position.
Centralizing it means that the above rule will be implemented once, not once
per target.
llvm-svn: 250421
After some additional post-commit (post-revert) discussion and research, this
reverts, in part, r250205, so the ABI-recommended starting address can be used
on PPC64 (as is done by other linkers).
Also, this addresses the FIXME in ELF/Writer.cpp by making VAStart a
target-dependent property.
llvm-svn: 250378
If an argument for --entry is a number, that's not a symbol name but
an absolute address. If that's the case, the address is directly set
to ELF header's e_entry.
llvm-svn: 250334
Previously, we used input section names as output section names.
That resulted that we created lots of sections for comdat
or -f{function,data}-section sections.
This patch reduces the number of sections by dropping suffix from
all section names which start with ".text.", ".rodata.", ".data."
or ".bss.". GNU linker does this using the internal linker script,
but for LLD I chose to do that directly.
Interestingly, this makes the linker faster. Time to link Clang
is this.
Before:
real 0m0.537s
user 0m0.433s
sys 0m0.104s
After:
real 0m0.390s
user 0m0.268s
sys 0m0.120s
It make sense because previously we created 57659 sections now only 27.
llvm-svn: 250315
- Make the `MipsTargetInfo` template class with `ELFType` argument. Use
the argument to select an appropriate relocation type and read/write
routines.
- Add template function `add32` to add-and-write relocation value in
both big and little endian cases. Keep the `add32le` to reduce code
changes.
Differential Revision: http://reviews.llvm.org/D13723
llvm-svn: 250297
The documentation says: "You may separate commands using semicolons",
so they seem to be optional.
Differential Revision: http://reviews.llvm.org/D13703
llvm-svn: 250223
Suggested by Rafael in his review of r250100. As Rafael points out, this may
grow into a switch in the future, but regardless, calling this on files for
other architectures is unnecessary.
llvm-svn: 250209
This has turned out to be unnecessary, and while some ability to set VAStart
will be needed at some point, this is not clearly the right direction.
llvm-svn: 250205
"finalize" does not give a hint about what that function is actually
going to do. This patch make it more specific by renaming scanShlibUndefined.
Also add a comment that we basically ignore undefined symbols in DSOs except
this function.
llvm-svn: 250191
The fix in r250109 to ensure a strict weak ordering in the section sorting was
a bit overzealous. We only use the NOBITS comparison if either A or B is a
NOBITS section. Otherwise, we fall through to the target-specific ranking
function. Failure to do this causes the sorting to fail in cases where, for
example, a .dynamic section happens to end up in between .got and .toc, etc. in
the initial ordering (.dynamic has a type SHT_DYNAMIC, compared to SHT_PROGBITS
or SHT_NOBITS).
llvm-svn: 250190
BSD's DSO files have undefined symbol "__progname" which is defined
in crt1.o. On that system, both user programs and system shared
libraries depend on each other.
In general, we need to put symbols defined by user programs which are
referenced by shared libraries to user program's .dynsym.
http://reviews.llvm.org/D13637
llvm-svn: 250176
What was done:
1) .got.plt section is created for functions that requires PLT. .got.plt has 3 predefined empty entries now that are required for dynamic linker.
Also other new items created are configured to have correct jump to PLT[N].
2) PLT section now has PLT[0] entry, also others ones are configured to support PLT->GOT(.got.plt) calls.
3) Implemented .rel[a].plt sections (based on patch http://reviews.llvm.org/D13569).
4) Fixed plt relocations types (based on patch http://reviews.llvm.org/D13589).
NOTES:
The .plt.got zero entry is still empty now. According to ELF specification it should hold the address of the dynamic structure, referenced with the symbol
_DYNAMIC. The _DYNAMIC entry points to the .dynamic section which contains information used by the ELF interpreter to setup the binary.
Differential Revision: http://reviews.llvm.org/D13651
llvm-svn: 250169
Now all Target<Arch> classes are used only in Target.cpp.
We can put them in an anonymous namespace. In order to avoid
merge conflict with other people's patches, I'll do that later.
llvm-svn: 250168
Under PPC64 ELF v1 ABI, the symbols associated with each function name don't
point directly to the code in the .text section (or similar), but rather to a
function descriptor structure in a special data section named .opd. The
elements in the .opd structure include a pointer to the actual code, and a the
relevant TOC base value. Both of these are themselves set by relocations.
When we have a local call, we need the relevant relocation to refer directly to
the target code, not to the function-descriptor in the .opd section. Only when
we have a .plt stub do we care about the address of the .opd function
descriptor itself.
So we make a few changes here:
1. Always write .opd first, so that its relocated data values are available
for later use when writing the text sections. Record a pointer to the .opd
structure, and its corresponding buffer.
2. When processing a relative branch relocation under ppc64, if the
destination points into the .opd section, read the code pointer out of the
function descriptor structure and use that instead.
This this, I can link, and run, a dynamically-compiled "hello world"
application on big-Endian PPC64/Linux (ELF v1 ABI) using lld.
llvm-svn: 250122
Under the PPC64 ELF ABI, functions that might call into other modules (and,
thus, need to load a different TOC base value into %r2), need to restore the
old value after the call. The old value is saved by the .plt code, and the
caller only needs to include a nop instruction after the call, which the linker
will transform into a TOC restore if necessary.
In order to do this the relocation handler needs two things:
1. It needs to know whether the call instruction it is modifying is targeting
a .plt stub that will load a new TOC base value (necessitating a restore after
the call).
2. It needs to know where the buffer ends, so that it does not accidentally
run off the end of the buffer when looking for the 'nop' instruction after the
call.
Given these two pieces of information, we can insert the restore instruction in
place of the following nop when necessary.
llvm-svn: 250110
As pointed out by Rui (post-commit review), we need to always return based on
the section type when the types differ to ensure a strict weak ordering.
llvm-svn: 250109
This is mostly an adaptation of the code in LLVM's
lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp, and handles a sufficient
number of relocations to link a 'hello world' program on big-Endian PPC64/Linux
(ELF v1 ABI).
llvm-svn: 250101
PPC64 has several special sections that are intended to be accessed from the
TOC base pointer. When a .got is present, the TOC base pointer is .got + 0x8000
(as specified by the ABI). Furthermore, the glibc startup code contains an
assumption that a 16-bit relocation can hold the offset from the TOC base value
to the beginning of the .toc section. Thus, we need to make sure that .toc
appears after .got. This much, at least, is required in practice. The other
PPC64 special sections (.toc, .toc1, .opd, etc.) should also be close by to
optimize access by smaller TOC-base-pointer offsets.
llvm-svn: 250100
In order to actually verify the condition, we have to use a weak symbol.
If an undefined symbol is not used at all, it is naturally ignored not by
the code for --as-needed but by the code for something else.
llvm-svn: 250062
This patch adds AsNeeded and IsUsed bool fields to SharedFile. AsNeeded bit
is set if the DSO is enclosed with --as-needed and --no-as-needed. IsUsed
bit is off by default. When we adds a symbol to the symbol table for dynamic
linking, we set its SharedFile's IsUsed bit.
If AsNeeded is set but IsUsed is not set, we don't want to write that
file's SO name to DT_NEEDED field.
http://reviews.llvm.org/D13579
llvm-svn: 249998
In the linker script, -l and = have the same meaning as in the command line.
In addition to that, if a path is not absolute, the path needs to be searched
from the search paths. This patch implements them.
llvm-svn: 249967
Previously, each ArgParser owned a BumpPtrAllocator, and arguments parsed
by an ArgParser would refer strings allocated using the BumpPtrAllocator
only when response files were used. This could cause a subtle bug because
such ownership was not obvious.
This patch moves the ownership from ArgParser to Driver and make the
ownership explicit.
llvm-svn: 249963
Previously, we reserved the first page for the program header, so no
sections would be written to very beginning of the output file.
FreeBSD requires .interp section be exist on the first page, so that
was not good.
This patch calculates the size of the program header and then assign
addresses to the sections. Now the first section is located next to
the program header. Because the first section is .interp, it's very
likely to be on the first page.
llvm-svn: 249957
SymbolTable was not a right place for initialization. We had to do that
because Driver didn't know what type of ELF objects are being handled.
We taught Driver that, so we can now move this code to Driver.
llvm-svn: 249904
SymbolTable was not a template class. Instead we had switch-case-based
type dispatch to call desired functions. We had to do that because
SymbolTable was created before we know what ELF type objects had been
passed.
Every time I tried to add a new function to the symbol table, I had to
define a dispatcher which consist of a single switch statement.
It also brought an restriction what the driver can do. For example,
we cannot add undefined symbols before any files are added to the symbol
table. That's because no symbols can be added until the symbol table
knows the ELF type, but when it knows about that, it's too late.
In this patch, the driver makes a decision on what ELF type objects
are being handled. Then the driver creates a SymbolTable object for
an appropriate ELF type.
http://reviews.llvm.org/D13544
llvm-svn: 249902
This reverts commit r249816.
It broke building llvm with lld:
$ ./bin/FileCheck
./bin/FileCheck: error while loading shared libraries: unexpected PLT reloc type 0x06
I think the only thing that is wrong with this patch is that it is too soon.
The plt we create (and its relocs) don't support lazy loading, so they have
to be relocated as ordinary dynamic relocations.
llvm-svn: 249835
.rela.plt contains list of elements in the PLT, which are liable to the relocation during the dynamic linking.
Differential Revision: http://reviews.llvm.org/D13569
llvm-svn: 249816
The required page alignment is different on different targets. On PowerPC, for
example, we need 64K pages (the loader won't set different permissions on a
finer granularity than that). I've set the existing targets to what I believe
to be the correct values, and have updated the regression tests accordingly.
llvm-svn: 249760
The size of a .plt entry is different on different targets (it is,
specifically, much larger than 8 on all PPC ABIs). There is no functional
change here (later patches to create .plt entries for PPC64 will depend on this
change).
llvm-svn: 249756
Reapply r249726 (and r249723), hopefully with the correct test fixups this time.
Original commit message:
Address a FIXME in ELF/Writer.cpp: Make VAStart a target-dependent property.
I've set the values for the existing targets to what I believe to be the
correct values, and updated the regression tests accordingly.
llvm-svn: 249752
Address a FIXME in ELF/Writer.cpp: Make VAStart a target-dependent property.
I've set the values for the existing targets to what I believe to be the
correct values, and updated the regression tests accordingly.
llvm-svn: 249723
In preparation for making the size of a .plt entry target dependent, use the
existing EntrySize variable when writing (instead of a hard-coded value). NFC.
llvm-svn: 249720
`not` command on Windows is not able to find an executable from PATH
if a given command already has an extension even if the extension is
not ".exe".
llvm-svn: 249630
Previously, output sections that are handled specially by the linker
(e.g. PLT or GOT) were created by Writer and passed to other classes
that need them. The problem was that because these special sections
are required by so many classes, the plumbing work became too much
burden.
This patch is to simply make them accessible from anywhere in the
linker to eliminate the plumbing work once and for all.
http://reviews.llvm.org/D13486
llvm-svn: 249590
When generating an executable or shared library, mark it to tell the dynamic linker to resolve all symbols when the program is started, or when the shared library is linked to using dlopen, instead of deferring function call resolution to the point when the function is first called.
Differential Revision: http://reviews.llvm.org/D13468
llvm-svn: 249551
Parse and apply emulation given with -m option.
Check input files to match ELF type and machine architecture provided with -m.
Differential Revision: http://reviews.llvm.org/D13055
llvm-svn: 249529
I am about to change lld to avoid creating unnecessary got entries for locally
defined symbols when creating executables.
Update tests that were depending on the current behavior.
llvm-svn: 249474
We were still fetching them when the archive was seen first.
We should experiment with just letting lazy symbols get to compare, it
might be cleaner for ELF.
llvm-svn: 249417
This is a case that requires --start-group --end-group with regular ELF
linkers. Fortunately it is still possible to handle it with lazy symbols without
taking a second look at archives.
Thanks to Michael Spencer for the bug report.
llvm-svn: 249406
This was clearly wrong (thanks Rui for spotting), and I honestly would
like to get this tested so such mistakes won't repeat. Unfortunately, I
wasn't (easily) able to craft a test that exposes the bad behavior.
Ideally, we would like to get tests of this kind for all relocations, but
at the time of writing, this is not true. So, for now just fix this bug
and try to re-evaluate a way to test this in the future.
llvm-svn: 249359
The entries are added if there are "_init" or "_fini" entries in
the symbol table respectively. According to the behavior of ld,
entries are inserted even for undefined symbols.
Symbol names can be overridden by using -init and -fini command
line switches. If used, these switches neither add new symbol table
entries nor require those symbols to be resolved.
Differential Revision: http://reviews.llvm.org/D13385
llvm-svn: 249297
Add symbol specified with -u as undefined which may cause additional
object files from archives to be linked into the resulting binary.
Differential Revision: http://reviews.llvm.org/D13345
llvm-svn: 249295
Using the "raw" Elf64_Dyn or Elf32_Dyn structures in
DynamicSection<ELFT>::writeTo does not correctly handle mixed-Endian
situations. Instead, use the corresponding llvm::object::* structures which
have Endian-converting members (like the rest of the code).
This fixes all currently-failing elf2 tests when running on big-Endian
PPC64/Linux (I've added a big-Endian test case which should fail on
little-Endian machines in the same way that test/elf2/shared.s failed on
big-Endian machines prior to this change).
llvm-svn: 249150
Found while testing a FreeBSD base system build with lld. Ignored for
now while we continue to identify missing options and functionality.
llvm-svn: 249144
Sort by:
ALLOC
ALLOC && NOBITS
ALLOC & EXEC
ALLOC & EXEC && NOBITS
ALLOC & WRITE
ALLOC & WRITE && NOBITS
<nothing> (ignoring NOBITS)
The dynamic section is finalized early because it adds strings to the dynamic string table, which comes before the dynamic table.
llvm-svn: 249071