Commit Graph

2058 Commits

Author SHA1 Message Date
Will Newton 0b7f842592 ELF: Use ELF reloc .def files to reduce duplication
Tested with check-lld with no regressions.

llvm-svn: 223462
2014-12-05 12:43:26 +00:00
Rui Ueyama c2576d5464 Remove extra semicolon.
llvm-svn: 223411
2014-12-04 23:26:11 +00:00
Eric Christopher e9a36eab1e Fix a bunch of -Winconsistent-missing-override warnings.
llvm-svn: 223400
2014-12-04 22:26:07 +00:00
Rui Ueyama 5ae2050420 Revert "Rewrite InputGraph's Group"
This reverts commit r223330 because it broke Darwin and ELF
linkers in a way that we couldn't have caught with the existing
test cases.

llvm-svn: 223373
2014-12-04 18:29:03 +00:00
Simon Atanasyan c9bcffd201 [ELF] Adjust ELF header entry symbol value if this symbol is microMIPS encoded
To find an AtomLayout object for the given symbol I replace the
`Layout::findAtomAddrByName` method by `Layout::findAtomLayoutByName` method.

llvm-svn: 223359
2014-12-04 13:43:35 +00:00
Rui Ueyama 0152732ef4 [PECOFF] Improve /export compatibility.
Looks like the rule of /export is more complicated than
I was thinking. If /export:foo, for example, is given, and
if the actual symbol name in an object file is _foo@<number>,
we need to export that symbol as foo, not as the mangled name.

If only /export:_foo@<number> is given, the symbol is exported
as _foo@<number>.

If both /export:foo and /export:_foo@<number> are given,
they are considered as duplicates, and the linker needs to
choose the unmangled name.

The basic idea seems that the linker needs to export a symbol
with the same name as given as /export.

We exported mangled symbols. This patch fixes that issue.

llvm-svn: 223341
2014-12-04 06:09:39 +00:00
Rui Ueyama 60df72ff61 Rewrite InputGraph's Group
The aim of this patch is to reduce the excessive abstraction from
the InputGraph. We found that even a simple thing, such as sorting
input files (Mach-O) or adding a new file to the input file list
(PE/COFF), is nearly impossible with the InputGraph abstraction,
because it hides too much information behind it. As a result,
we invented complex interactions between components (e.g.
notifyProgress() mechanism) and tricky code to work around that
limitation. There were many occasions that we needed to write
awkward code.

This patch is a first step to make it cleaner. As a first step,
this removes Group class from the InputGraph. The grouping feature
is now directly handled by the Resolver. notifyProgress is removed
since we no longer need that. I could have cleaned it up even more,
but in order to keep the patch minimum, I focused on Group.

SimpleFileNode class, a container of File objects, is now limited
to have only one File. We shold have done this earlier.
We used to allow putting multiple File objects to FileNode.
Although SimpleFileNode usually has only one file, the Driver class
actually used that capability. I modified the Driver class a bit,
so that one FileNode is created for each input File.

We should now probably remove SimpleFileNode and directly store
File objects to the InputGraph in some way, because a container
that can contain only one object is useless. This is a TODO.

Mach-O input files are now sorted before they are passe to the
Resolver. DarwinInputGraph class is no longer needed, so removed.

PECOFF still has hacky code to add a new file to the input file list.
This will be cleaned up in another patch.

llvm-svn: 223330
2014-12-04 01:09:06 +00:00
Rui Ueyama a5e2a66dda [PECOFF] Improve compatibility of /export option.
llvm-svn: 223326
2014-12-04 00:31:34 +00:00
Rui Ueyama d31cf6065f [PECOFF] Fix a bug in /export option handler.
/export option can be given multiple times to specify multiple
symbols to be exported. /export accepts both decorated and
undecorated name.

If you give both undecorated and decorated name of the same symbol
to /export, they are resolved to the same symbol. In this case,
we need to de-duplicate the exported names, so that we don't have
duplicated items in the export symbol table in a DLL.

We remove duplicate items from a vector. The bug was there.
Because we had pointers pointing to elements of the vector,
after an item is removed, they would point wrong elements.

This patch is to remove these pointers. Added a test for that case.

llvm-svn: 223200
2014-12-03 04:34:20 +00:00
Nick Kledzik 07ba5121a1 [mach-o] Fix TrieEdge leak
In PR21682 Jean-Daliel Dupas found a leak in the trie builder and suggested
a fix was to use a list instead of SmallVector so that the list elements
could be allocated in the BumpPtrAllocator.

llvm-svn: 223104
2014-12-02 01:50:44 +00:00
Nick Kledzik a441b7b050 [mach-o] add support for arm64 compact unwind info
Tim previously added generic compact unwind processing and x86_64 support.
This patch adds arm64 support.

llvm-svn: 223103
2014-12-02 01:50:38 +00:00
Shankar Easwaran 7a64ec24d6 [Core] Remove function to not override RoundTripPasses.
RoundTripPasses should always be called in DEBUG mode if the environment
variable "LLD_RUN_ROUNDTRIP_TEST" is set.

Flavors should not be able to override this behavior.

llvm-svn: 223073
2014-12-01 20:28:54 +00:00
Shankar Easwaran 2895527ec2 [Core] Add flag to check if RoundTripPasses need to be run.
This would allow other flavor specific contexts to override the default value,
if they want to optionally run the round trip passes.

There is some information lost like the original file owner of the atom with
RoundTripPasses. The Gnu flavor needs this information inorder to implement
LinkerScript matching and for other diagnostic outputs such as Map files.

The flag also can be used to record information in the Atom if the information
to the Writer needs to be conveyed through References too.

llvm-svn: 222983
2014-12-01 01:04:11 +00:00
Shankar Easwaran a0184368d6 [ELF] Fix layout of output sections.
The AtomSections were improperly merging sections from various input files. This
patch fixes the problem, with an updated test that was provided by Simon.

Thanks to Simon Atanasyan for catching this issue.

llvm-svn: 222982
2014-11-30 22:36:29 +00:00
Shankar Easwaran 52a53fe591 [ELF] Rename align2 to alignment.
No change in functionality.

llvm-svn: 222975
2014-11-30 04:17:06 +00:00
Shankar Easwaran 25c33d60c2 [ELF] Fix creation of segments.
Linker was creating a separate output segment in some cases if input sections
had huge alignments. This patch fixes the issue.

llvm-svn: 222974
2014-11-30 04:01:14 +00:00
Shankar Easwaran aa00539aa5 [ELF] Create input and output section names
No change in functionality.

llvm-svn: 222973
2014-11-30 03:43:38 +00:00
Shankar Easwaran 2d382c6943 [ELF] Rename MergedSection to OutputSection.
No change in functionality.

llvm-svn: 222972
2014-11-30 03:18:08 +00:00
Will Newton 2db4107a36 Rename R_AARCH64_TLSDESC_ADR_PAGE to R_AARCH64_TLSDESC_ADR_PAGE21
llvm-svn: 222823
2014-11-26 11:00:03 +00:00
Will Newton ee5772faaa Fix comment in AArch64 ELF backend
llvm-svn: 222814
2014-11-26 09:51:54 +00:00
Rui Ueyama b12191aaf9 Use SafelyCloseFileDescriptor instead of close.
Opening a file using openFileForWrite and closing it using close
was asymmetric. It also had a subtle portability problem (details are
described in the commit message for r219189).

llvm-svn: 222802
2014-11-26 01:45:24 +00:00
Rui Ueyama 65e0ca1bde [PECOFF] Properly close a file descriptor.
This was basically benign resource leak on Unix, but on Windows
it could cause builds to fail because opened file descriptor
prevents other processes from moving or removing the file.

llvm-svn: 222799
2014-11-26 00:21:27 +00:00
Rui Ueyama 24a7f7eed8 [PECOFF] Ignore /maxilkfile option.
.ilk file is a file for incremental linking. We don't create nor use
that file.

/MAXILKFILE is an undocumented option to specify the maximum size
of the .ilk file, IIUC. We should just ignore the option.

llvm-svn: 222777
2014-11-25 19:51:28 +00:00
Rui Ueyama 0d9a181d9d [PECOFF] Create an empty PDB file if debug option is enabled.
There are many build files in the wild that depend on the fact that
link.exe produces a PDB file if /DEBUG option is given. They fail
if the file is not created.

This patch is to make LLD create an empty (dummy) file to satisfy
such build targets. This doesn't do anything other than "touching"
the file.

If a target depends on the content of the PDB file, this workaround
is no help, of course. Otherwise this patch should help build some
stuff.

llvm-svn: 222773
2014-11-25 19:01:01 +00:00
David Majnemer 86903b70fc Silence a -Wcast-qual warning
llvm-svn: 222598
2014-11-22 00:45:01 +00:00
Simon Atanasyan cff9efb52f [Mips] Use endian::read() for relocation addendum reading
llvm-svn: 222574
2014-11-21 21:26:32 +00:00
Rui Ueyama 4a259cf4a5 [PECOFF] Enable dead-stripping even if /debug option is given.
/debug makes MSVC link.exe to not remove unused sections from
the resulting executable. We did the same thing before. However,
I realized that the removal of associative section depends on
the dead-stripping pass in LLD, so we cannot disable that. Or
LLD may produce slightly broken executables that have too much
data in it (which could result in nasty subtle bugs).

This patch is a temporary measure to create correct executable.
Currently /debug does not have any real effect for LLD anyway.

I'll improve associative section handling in another patch, so that
they are removed from output without depending on the dead-stripping
pass.

llvm-svn: 222483
2014-11-20 23:59:40 +00:00
Simon Atanasyan 8489349fdc [Mips] Simplify the code calculates HI16/LO16 relocations
No functional changes.

llvm-svn: 222470
2014-11-20 22:29:55 +00:00
Simon Atanasyan f2da06b774 [Mips] Remove duplicated relocation calculation routines
No functional changes.

llvm-svn: 222469
2014-11-20 22:29:49 +00:00
Simon Atanasyan de672148dd [Mips] Remove unused function argument
No functional changes.

llvm-svn: 222468
2014-11-20 22:29:43 +00:00
Simon Atanasyan 8a8282a8bb [Mips] Use endian::read/write function to load/save MIPS32 instructions
No functional changes.

llvm-svn: 222467
2014-11-20 22:29:37 +00:00
Nick Kledzik f78c64c6ae [mach-o] fix whitespace
llvm-svn: 222457
2014-11-20 21:41:53 +00:00
Nick Kledzik 9d98f7e454 [mach-o] use reference with "auto" to prevent copies
Patch by Jean-Daniel Dupas

llvm-svn: 222455
2014-11-20 21:19:58 +00:00
Rui Ueyama 2343e65b14 [PECOFF] Sort export table properly.
Export table entries need to be sorted in ASCII-betical order,
so that the loader can find an entry for a function by binary search.

We sorted the entries by its mangled names. That can be different
from their exported names. As a result, LLD produces incorrect export
table, from which the loader complains that a function that actually
exists in a DLL cannot be found.

This patch fixes that issue.

llvm-svn: 222452
2014-11-20 21:05:05 +00:00
Simon Atanasyan eeea5f88aa [Mips] Configure GOTPLT entries in the corresponding constructor
No functional changes.

llvm-svn: 222415
2014-11-20 06:34:04 +00:00
Shankar Easwaran dc23dad2b7 Revert "[ELF] Sectionkey should also contain path."
This reverts commit r222309.

Reverting because of failures on darwin bot.

llvm-svn: 222332
2014-11-19 06:47:29 +00:00
Shankar Easwaran b508eb269f Revert "[ELF] Rename MergedSection to OutputSection."
This reverts commit r222310.

Not sure which commit is the cause of the failure on the darwin bot. Will need
to revert my changes and commit one change at a time.

llvm-svn: 222330
2014-11-19 06:24:53 +00:00
Shankar Easwaran f994868b65 Revert "[ELF] Create input and output section names"
This reverts commit r222311.

Reverting because of failure in the darwin bot.

llvm-svn: 222329
2014-11-19 06:24:51 +00:00
Shankar Easwaran f552996fb3 Revert "[ELF] Fix creation of segments."
This reverts commit r222312.

There looks to be an intermittent failure in the darwin buildbot.

llvm-svn: 222326
2014-11-19 05:53:13 +00:00
Simon Atanasyan bd7187c8d4 [Mips] Remove unnecessary debug logging code
No functional changes

llvm-svn: 222325
2014-11-19 05:51:16 +00:00
Simon Atanasyan 863f32f70d [Mips] Configure PLT and LA25 entries in the corresponding constructors
No functional changes.

llvm-svn: 222324
2014-11-19 05:51:10 +00:00
Simon Atanasyan a50af747eb [Mips] Inline the configurePLTReference method
No functional changes.

llvm-svn: 222323
2014-11-19 05:51:05 +00:00
Simon Atanasyan 6a33f94dd1 [Mips] Remove redundant const_cast
No functional changes.

llvm-svn: 222322
2014-11-19 05:50:59 +00:00
Simon Atanasyan cd69b04381 [Mips] Make the function return type constant pointer
No functional changes.

llvm-svn: 222321
2014-11-19 05:50:53 +00:00
Simon Atanasyan ff903f385c [Mips] Move setup of LLD_R_MIPS_STO_PLT relocation to the getPLTEntry() routine
No functional changes.

llvm-svn: 222320
2014-11-19 05:50:47 +00:00
Shankar Easwaran 2d946283f9 [ELF] Handle ctors/dtors sections
Move functionality from MIPS target to DefaultLayout.

No change in functionality.

llvm-svn: 222313
2014-11-19 03:51:50 +00:00
Shankar Easwaran 294cca1e8c [ELF] Fix creation of segments.
Linker was creating a separate output segment in some cases if input sections
had huge alignments. This patch fixes the issue.

llvm-svn: 222312
2014-11-19 03:51:48 +00:00
Shankar Easwaran e35660ebe7 [ELF] Create input and output section names
No change in functionality.

llvm-svn: 222311
2014-11-19 03:51:45 +00:00
Shankar Easwaran 4eb1f9c01f [ELF] Rename MergedSection to OutputSection.
No change in functionality.

llvm-svn: 222310
2014-11-19 03:51:43 +00:00
Shankar Easwaran e490f38a5b [ELF] Sectionkey should also contain path.
No change in functionality.

llvm-svn: 222309
2014-11-19 03:51:41 +00:00
Nick Kledzik 5b9e48b4ce [mach-o] propagate dylib version numbers
Mach-o does not use a simple SO_NEEDED to track dependent dylibs.  Instead,
the linker copies four things from each dylib to each client: the runtime path
(aka "install name"), the build time, current version (dylib build number), and
compatibility version  The build time is no longer used (it cause every rebuild
of a dylib to be different).  The compatibility version is usually just 1.0
and never changes, or the dylib becomes incompatible.

This patch copies that information into the NormalizedMachO format and
propagates it to clients.

llvm-svn: 222300
2014-11-19 02:21:53 +00:00
Rui Ueyama 81b73f3020 Fix MSVC warning.
This patch fixes the following MSVC warning.

  warning C4334: '<<' : result of 32-bit shift implicitly
  converted to 64 bits (was 64-bit shift intended?)

llvm-svn: 222293
2014-11-19 00:48:06 +00:00
Nick Kledzik 2020efb9fe [mach-o] for 32-bit arm, in -r mode support B (not BL) to undefined symbol
When fixing up BL instructions, the linker has to compare the thumbness of the
target to decide if the instruction needs to be converted to BLX.  But with B
instruction there is no BX, so the linker asserts if the target is not the
same thumbness. This assert was firing in -r mode when the target was undefined
which it interpreted as being non-thumb.

Test case change is to add a B (in both thumb and arm code) to an undefined
symbol and round trip through -r mode.

llvm-svn: 222266
2014-11-18 22:08:50 +00:00
Nick Kledzik b072c3673a [mach-o] zero-fill sections have no file offset
In mach-o, sections of type S_ZEROFILL are special cased and to always have
their section.offset field be zero.

llvm-svn: 222202
2014-11-18 00:30:29 +00:00
Nick Kledzik 63adb08c2c [mach-o] extract atom alignment information from mach-o files
llvm-svn: 222201
2014-11-18 00:30:25 +00:00
Nick Kledzik f6e5ace974 [mach-o] enhance arm64 reloc parser to support local pointer relocations
The arm64 assembler almost always uses r_extern=1 relocations in which the
r_symbolnum field is the index of the symbol the relocation references.  But
sometimes it will set r_extern=0 in which case the linker needs to read the
content of the reloction to determine the target.

Add test case that the r_extern=0 relocation round trips.

llvm-svn: 222200
2014-11-18 00:30:22 +00:00
Nick Kledzik 68c42ecae0 [mach-o] fix typos. No function change
llvm-svn: 222199
2014-11-18 00:30:20 +00:00
Nick Kledzik 60db3549e2 [mach-o] enhance arm64 reloc parser to support local pointer relocations
The arm64 assembler almost always uses r_extern=1 relocations in which the
r_symbolnum field is the index of the symbol the relocation references.  But
sometimes it will set r_extern=0 in which case the linker needs to read the
content of the reloction to determine the target.

Add test case that the r_extern=0 relocation round trips.

llvm-svn: 222198
2014-11-18 00:30:17 +00:00
Nick Kledzik 218f7199ca [mach-o] fix typos. No function change
llvm-svn: 222197
2014-11-18 00:30:14 +00:00
Rui Ueyama 5552de51d5 [PECOFF] Fix 32-bit delay-import table.
llvm-svn: 222116
2014-11-17 02:04:54 +00:00
Rui Ueyama 71aa1a9355 [PECOFF] Fix section alignment.
If you have something like

  __declspec(align(8192)) int foo = 1;

in your code, the compiler makes the data to be aligned to 8192-byte
boundary, and the linker align the section containing the data to 8192.

LLD always aligned the section to 4192. So, as long as alignment
requirement is smaller than 4192, it was correct, but for larger
requirements, it's wrong.

This patch fixes the issue.

llvm-svn: 222043
2014-11-14 21:33:07 +00:00
Rui Ueyama ad8188e0b6 [PECOFF] Remove dead code
AddressOfEntryPoint is overridden after we layout all atoms (until then,
we don't know the entry point address for obvious reason.)
I believe this code is leftover from very early version of the
PE/COFF port that we only had an entry function in a test object file.

llvm-svn: 222026
2014-11-14 19:21:06 +00:00
Eric Christopher fe455041b9 Silence inconsistent override warnings.
llvm-svn: 222021
2014-11-14 18:44:53 +00:00
Simon Atanasyan 55c2699d29 Follow-up to r221913. Fix some -Wcast-qual warning reasons.
llvm-svn: 221974
2014-11-14 07:15:43 +00:00
Shankar Easwaran c37f8af577 [ELF] Dynamic section was not aligned properly.
The dynamic section was not aligned properly. The alignment of the section is
determined by the word size of the architecture.

llvm-svn: 221972
2014-11-14 04:57:21 +00:00
Shankar Easwaran 0769c0ed0d [ELF] Fix the fileoffset according to the ELF spec.
With --no-align-segments, there is a bug that the fileoffset may not be
congruent to virtual address modulo page alignment.

This patch fixes the problem.

llvm-svn: 221890
2014-11-13 14:30:21 +00:00
Simon Atanasyan c77f5fa4f4 [ELF] Add CodeModel attribute to the DefinedAtom class
MIPS ELF symbols might contain some additional MIPS-specific flags
in the st_other field besides visibility ones. These flags indicate
code properties like microMIPS / MIPS16 encoding, position independent
code etc. We need to transfer the flags from input objects to the
output linked file to write them into the symbol table, adjust symbols
addresses etc.

I add new attribute CodeModel to the DefinedAtom class to hold target
specific flag and to get over YAML/Native format conversion barrier.
Other architectures/targets can extend CodeModel enumeration by their
own flags.

MIPS specific part of this patch adds support for STO_MIPS_MICROMIPS
flag. This flag marks microMIPS symbols. Such symbol should:
a) Has STO_MIPS_MICROMIPS in the corresponding .symtab record.
b) Has adjusted (odd) address in the corresponding .symtab
   and .dynsym records.

llvm-svn: 221864
2014-11-13 07:03:41 +00:00
Shankar Easwaran 74ce86cb83 [ELF] Fix segment alignment.
The segment alignment for PT_LOAD segments is set to page size by default, but
if any of the sections require an alignment more than the page size, the segment
alignment property is set to the maximum alignment of the sections that are part
of the segment.

llvm-svn: 221862
2014-11-13 04:33:06 +00:00
Shankar Easwaran 2e20049260 [ELF] Fix conditions for max-page-size.
Fix comments from Rui, also adds a test.

llvm-svn: 221860
2014-11-13 04:03:46 +00:00
Shankar Easwaran b43804b6f4 [ELF] Fix max-page-size option.
The user can use the max-page-size option and set the maximum page size. Dont
check for maximum allowed values for page size, as its what the kernel is
configured with.

Fix the test as well.

llvm-svn: 221858
2014-11-13 03:25:38 +00:00
Rui Ueyama 904cf593c1 [PECOFF] Fix delay-import address table contents.
Each entry in the delay-import address table had a wrong alignment
requirement if 32 bit. As a result it got wrong delay-import table.

Because llvm-readobj doesn't print out that field, we don't have a
test for that. I'll submit a test that would catch this bug after
improving llvm-readobj.

llvm-svn: 221853
2014-11-13 02:58:36 +00:00
Nick Kledzik 8870ad2439 [mach-o] Sort GOT entries by name to make links reproducible
The GOT slots were being laid out in a random order by the GOTPass which
caused randomness in the output file.

Note: With this change lld now bootstraps on darwin.  That is:
1) link lld using system linker to make lld.1
2) link lld using lld.1 to make lld.2
3) link lld using lld.2 to make lld.3
Now lld.2 and lld.3 are identical.

llvm-svn: 221831
2014-11-12 23:34:23 +00:00
Nick Kledzik 16cfa60cbd [mach-o] Support linker synthesized mach_header symbols.
On darwin in final linked images, the __TEXT segment covers that start of the
file.  That means in memory a process can see the mach_header (and load commands)
for every loaded image in a process.  There are APIs that take and return the
mach_header addresses as a way to specify a particular loaded image.

For completeness, any code can get the address of the mach_header of the image
it is in by using &__dso_handle.  In addition there are mach-o type specific
symbols like __mh_execute_header.

The linker needs to supply a definition for any of these symbols if used.  But
the address the symbol it resolves to is not in any section.  Instead it is the
address of the start of the __TEXT segment.

I needed to make a small change to SimpleFileNode to not override
resetNextIndex() because the Driver creates a SimpleFileNode to hold the
internal/implicit files that the context/writer can create. For some reason
SimpleFileNode overrode resetNextIndex() to do nothing instead of reseting
the index (which mach-o needs if the internal file is an archive).

llvm-svn: 221822
2014-11-12 22:21:56 +00:00
Nick Kledzik f373c77f50 [mach-o] Fix lazy binding offsets
The way lazy binding works in mach-o is that the linker generates a helper
function and has the stub (PLT) initially jump to it.  The helper function
pushes an extra parameter then jumps into dyld.  The extra parameter is an
offset into the lazy binding info where dyld will find the information about
which symbol to bind and way lazy binding pointer to update.

llvm-svn: 221654
2014-11-11 01:31:18 +00:00
Shankar Easwaran 0becd57044 [ELF] Change order of section match.
Addressed comments from Sean silva.

llvm-svn: 221650
2014-11-11 00:40:38 +00:00
Shankar Easwaran 48b9fc7981 Revert "Add support library."
This reverts commit r221583.

llvm-svn: 221649
2014-11-11 00:40:36 +00:00
Shankar Easwaran 1987862287 [Gnu][Driver] Use StringRef conversion functions
llvm-svn: 221648
2014-11-11 00:40:32 +00:00
Simon Atanasyan 7b2e2b7d01 [Mips] Replace the redundant condition by assert call
No functional changes.

llvm-svn: 221603
2014-11-10 15:50:22 +00:00
Simon Atanasyan 1255354141 [Mips] Do not read addends for relocations which do not use them
No functional changes.

llvm-svn: 221602
2014-11-10 15:50:01 +00:00
Shankar Easwaran 7757f1ace6 [Gnu] Support --image-base option
The value for --image-base is used as the base address of the program.

llvm-svn: 221589
2014-11-10 14:55:21 +00:00
Shankar Easwaran 7a82510348 [ELF] Fix DT_INIT_ARRAY{SZ} and DT_FINI_ARRAY{SZ}
The dynamic table was creating the entry DT_FINI_ARRAY{SZ} even when there was
no .fini_array section. The entries should be creating in the dynamic section
only if there are sections .init_array/.fini_array in the output.

Fixes the tests that checked for errroneous outputs.

llvm-svn: 221588
2014-11-10 14:55:11 +00:00
Shankar Easwaran 0abfc278f0 [ELF] Fix values of linker created dynamic variables.
The value of _DYNAMIC should be pointing at the start of the .dynamic segment.
This was pointing to the end of the dynamic segment. Similarly the value of
_GLOBAL_OFFSET_TABLE_ was not proper too.

llvm-svn: 221587
2014-11-10 14:55:07 +00:00
Shankar Easwaran 38d187021f [ELF] .dynamic should have SHT_DYNAMIC flag
llvm-svn: 221586
2014-11-10 14:54:53 +00:00
Shankar Easwaran 78aa81d6ed [Gnu] Add options that are ignored
Add options that are ignored and exists just for compatibility reasons.

llvm-svn: 221585
2014-11-10 14:54:49 +00:00
Shankar Easwaran ba154afa06 [ELF] Support -z max-page-size option
The GNU linker allows the user to change the page size by using the option -z
max-page-size.

llvm-svn: 221584
2014-11-10 14:54:43 +00:00
Shankar Easwaran c937b4923e Add support library.
The parsing routines in the linker script to parse strings encoded in various
formats(hexadecimal, octal, decimal, etc), is needed by the GNU driver too. This
library provides helper functions for all flavors and flavors to add helper
functions which other flavors may make use of.

llvm-svn: 221583
2014-11-10 14:54:34 +00:00
Shankar Easwaran 8f6dd44056 [ELF] Remove unused variable
llvm-svn: 221572
2014-11-08 04:03:24 +00:00
Shankar Easwaran 22c76a5d79 [ELF] Support --no-align-segments.
lld generates an ELF by adhering to the ELF spec by aligning vma/fileoffset to a
page boundary, but this becomes an issue when dealing with large pages. This
adds support so that lld generated executables adheres to the ELF spec with the
rule vma % p_align = offset % p_align.

This is supported by the flag --no-align-segments.

This could be the default in few targets like X86_64 to save space on disk.

llvm-svn: 221571
2014-11-08 03:44:49 +00:00
Nick Kledzik 466f80f1fc [mach-o] remove stray debug output
llvm-svn: 221553
2014-11-07 22:00:26 +00:00
Nick Kledzik f04ab0a244 [mach-o] Fix MachOFileNode to own archives same as ELFFileNode
My previous fix to have FileArchive own the member MemoryBuffers was not a
complete solution for darwin because nothing owned the FileArchive object.
Fixed MachOFileNode to be like ELFFileNode and have the graph node own the
archive object.

llvm-svn: 221552
2014-11-07 22:00:23 +00:00
Nick Kledzik 82d24bc932 [mach-o] Add support for -order_file option
The darwin linker lets you rearrange functions and data for better locality
(less paging).  You do this with the -order_file option which supplies a text
file containing one symbol per line.

Implementing this required a small change to LayoutPass to add a custom sorter
hook.

llvm-svn: 221545
2014-11-07 21:01:21 +00:00
Nick Kledzik 7bcfe288a8 Fix FileArchive member MemoryBuffer early destruction
When FileArchive loads a member, it instantiates a temporary MemoryBuffer
which points to the member range of the archive file.  The problem is that the
object file parsers call getBufferIndentifer() on that temporary MemoryBuffer
and store that StringRef as the _path data member for that lld::File.  When
FileArchive::instantiateMember() goes out of scope the MemoryBuffer is deleted
and the File::._path becomes a dangling reference.

The fix adds a vector<> to FileArchive to own the instantiated MemoryBuffers.
In addition it fixes member's path to be the standard format
(e.g. "/path/libfoo.a(foo.o)") instead of just the leaf name.

llvm-svn: 221544
2014-11-07 20:52:38 +00:00
Simon Atanasyan e77029cc5b [Mips] Do not hard-code the paired relocation type
Request `getPairRelocation()` function to get paired relocation type.
That allows us to look up another pairs like R_MICROMIPS_HI16/LO16
in the future.

llvm-svn: 221539
2014-11-07 19:32:08 +00:00
Shankar Easwaran 35cab83409 [ELF] Remove is64bits() and isLittlEndian().
ELFLinkingContext had these two functions, which is really not needed since
the Writer uses a llvm::object template composed of Endianness, Alignment,
Is32bit/64bit. We could just use that and not duplicate functionality.

No Change In Functionality.

llvm-svn: 221523
2014-11-07 14:08:43 +00:00
Rui Ueyama 2d220ab9b4 [PECOFF] Improve subsystem inference
If /subsystem option is not given, the linker needs to infer the
subsystem based on the entry point symbol. If it fails to infer
that, the linker should error out on it.

LLD was almost correct, but it would fail to infer the subsystem
if the entry point is specified with /entry. This is because the
subsystem inference was coupled with the entry point function
searching (if no entry point name is specified, the linker needs
to find the right entry name).

This patch makes the subsystem inference an independent pass to
fix the issue. Now, as long as an entry point function is defined,
LLD can infer the subsystem no matter how it resolved the entry
point.

I don't think scanning all the defined symbols is fast, although
it shouldn't be that slow. The file class there does not provide
any easy way to find an atom by name, so this is what we can do
at this moment. I'd like to revisit this later to make it more
efficient.

llvm-svn: 221499
2014-11-06 23:50:48 +00:00
Nick Kledzik babee83257 remove unneeded { }
llvm-svn: 221478
2014-11-06 19:33:59 +00:00
Nick Kledzik 24f504001d [mach-o] Add support for -force_load option
The darwin linker has two ways to force all members of an archive to be loaded.
The -all_load option applies to all static libraries.  The -force_load takes
a path to a library and just that library's members are force loaded.

llvm-svn: 221477
2014-11-06 19:33:57 +00:00
Simon Atanasyan c88db09cc5 [Mips] Take into account that PIC code is inherently CPIC
Follow-up to r221439.

llvm-svn: 221442
2014-11-06 07:07:29 +00:00
Simon Atanasyan e4f47f08a3 [Mips] Follow-up to r221439. Include header to fix build on Windows.
llvm-svn: 221440
2014-11-06 06:28:40 +00:00
Simon Atanasyan b915d07a8e [Mips] Check ELF flags to prevent linking of incompatible files
1. The path checks ELF header flags to prevent linking of incompatible files.
   For example we do not allow to link files with different ABI, -mnan
   flags, some combination of target CPU etc.
2. The patch merge ELF header flags from input object files to put their
   combination to the generated file. For example, if some input files
   have EF_MIPS_NOREORDER flag we need to put this flag to the output
   file header.

I use the `parseFile()` (not `canParse()`) method because in case of
recognition of incorrect input flags combination we should show detailed
error message and stop the linking process and should not try to use
another `Reader`.

llvm-svn: 221439
2014-11-06 05:53:10 +00:00
Shankar Easwaran 4446829883 [ELF] Remove llvm::Twine usage
llvm-svn: 221433
2014-11-06 03:03:58 +00:00
Nick Kledzik 8f75da0db3 [mach-o] Add support for -S option
The darwin linker does not process dwarf debug info.  Instead it produces a
"debug map" in the output file which points back to the original .o files for
anything that wants debug info (e.g. debugger).

The -S option means "don't add a debug map".  lld for mach-o currently does
not generate the debug map, so there is nothing to do when this option is used.
But we need to process the option to get existing projects building.

llvm-svn: 221432
2014-11-06 03:03:42 +00:00
Shankar Easwaran 7fea87cb59 [ELF] Remove includes that are not used
llvm-svn: 221430
2014-11-06 02:35:42 +00:00
Shankar Easwaran 2dfef4bfd6 [ELF] Use llvm::Twine
llvm-svn: 221428
2014-11-06 02:03:40 +00:00
Shankar Easwaran e27c59d9e5 [ELF] Implement isLittleEndian for all architectures
llvm-svn: 221427
2014-11-06 02:03:38 +00:00
Shankar Easwaran 0c501df3de [ELF] Use std::find_if instead
llvm-svn: 221426
2014-11-06 02:03:35 +00:00
Nick Kledzik 37d1aa715a [mach-o] remove extra leading underscore on __bss and __got
llvm-svn: 221425
2014-11-06 01:14:12 +00:00
Rui Ueyama 58942cc8ec [PECOFF] Add a comment for SECREL relocation.
llvm-svn: 221423
2014-11-06 01:14:02 +00:00
Nick Kledzik 5c4693d8d2 [mach-o] Add support for interposing tuples section
Darwin uses two-level-namespace lookup for symbols which means the static
linker records where each symbol must be found at runtime.  Thus defining a
symbol in a dylib loaded earlier will not effect where symbols needed by
later dylibs will be found.  Instead overriding is done through a section
of type S_INTERPOSING which contains tuples of <interposer, interposee>.

llvm-svn: 221421
2014-11-06 01:09:13 +00:00
Rui Ueyama 30804c4220 [PECOFF] Fix SECREL relocations.
SECREL relocation's value is the offset to the beginning of the section.
Because of the off-by-one error, if a SECREL relocation target is at the
beginning of a section, it got wrong value.

Added a test that would have caught this.

llvm-svn: 221420
2014-11-06 01:03:23 +00:00
Nick Kledzik 80118548d2 Fix shadowed variable warning
llvm-svn: 221414
2014-11-05 23:40:04 +00:00
David Majnemer 31fb9edc00 MachO: Remove an unused variable from processSection
The local variable `cfi` became dead in r220730 when it's use was
obviated; it was replaced with a call to read32.

No functionality change intended.

llvm-svn: 221412
2014-11-05 23:22:41 +00:00
Simon Atanasyan 7b1a5af229 [ELF] Use getVisibility() function to access st_other field
The st_other field can contains not only visibility flag so we should
retrieve a visibility flag using a bit-mask.

llvm-svn: 221397
2014-11-05 21:09:53 +00:00
David Majnemer ff9848ab08 PECOFF: Set the AddressOfRelocationTable in the DOS header
Many programs, for reasons unknown, really like to look at the
AddressOfRelocationTable to determine whether or not they are looking at
a bona fide PE file.  Without this, programs like the UNIX `file'
utility will insist that they are looking at a MS DOS executable.

llvm-svn: 221335
2014-11-05 06:37:08 +00:00
Rui Ueyama cace52091b [PECOFF] Do not skip COMDAT section symbols.
LLD skipped COMDAT section symbols when reading them because
I thought we don't want to have symbols with the same name.
But they are actually needed because relocations may refer to
the section symbols. So we shoulnd't skip them.

llvm-svn: 221329
2014-11-05 02:21:39 +00:00
Rui Ueyama 4f5cbc1a1e [PECOFF] Fix symbols in module-definition file.
llvm-svn: 221303
2014-11-04 22:09:13 +00:00
Tim Northover f98b1c9960 [mach-o] remove __compact_unwind atoms once __unwind_info has been generated
The job of the CompactUnwind pass is to turn __compact_unwind data (and
__eh_frame) into the compressed final form in __unwind_info. After it's done,
the original atoms are no longer relevant and should be deleted (they cause
problems during actual execution, quite apart from the fact that they're not
needed).

llvm-svn: 221301
2014-11-04 21:57:32 +00:00
Rafael Espindola 5a899e332b Use llvm::sys::findProgramByName. NFC.
llvm-svn: 221257
2014-11-04 12:34:32 +00:00
Shankar Easwaran 1baf2359df [ELF] Update TODO
llvm-svn: 221235
2014-11-04 04:50:58 +00:00
Shankar Easwaran 4e9b8702f5 [ELF] Fix MSVC buildbot.
For some reason, MSVC doesnot like the style.

Checking to make sure this fixes the problem with the build.

llvm-svn: 221234
2014-11-04 04:25:00 +00:00
Shankar Easwaran f56205bb8e [ELF] Fix program headers.
The ELF writer creates a invalid binary for few cases with large filesize and
memory size for segments. This patch addresses the functionality and updates the
test. This patch also cleans up parts of the ELF writer for future enhancements
to support Linker scripts.

llvm-svn: 221233
2014-11-04 03:57:04 +00:00
David Majnemer 4eb0a3fd25 PECOFF: Use the string table for long section names in EXEs/DLLs
Normally, PE files have section names of eight characters or less.
However, this is problematic for DWARF because DWARF section names are
things like .debug_aranges.

Instead of truncating the section name, redirect the section name into
the string table.

Differential Revision: http://reviews.llvm.org/D6104

llvm-svn: 221212
2014-11-04 00:53:57 +00:00
Rafael Espindola b8ceaf15cd Fix leak found by asan.
llvm-svn: 221180
2014-11-03 20:55:23 +00:00
Rafael Espindola 1f9a45fdfb Fix a leak found by asan.
llvm-svn: 221179
2014-11-03 20:49:17 +00:00
Rafael Espindola fd5d365463 Fix warnings about missing override.
llvm-svn: 221165
2014-11-03 17:14:46 +00:00
Rafael Auler d301b9b3e0 [LinkerScript] Change ErrorOr usage to fix MSVC2012 buildbots
Number parsing functions used an ErrorOr<> idiom that is not supported in
MSVC2012. This patch fixes this.

llvm-svn: 221128
2014-11-03 05:26:18 +00:00
Rafael Auler 347fb02093 [lld] Teach LLD how to parse most linker scripts
This patch does *not* implement any semantic actions, but it is a first step to
teach LLD how to read complete linker scripts. The additional linker scripts
statements whose parsing is now supported are:

* SEARCH_DIR directive
* SECTIONS directive
* Symbol definitions inside SECTIONS including PROVIDE and PROVIDE_HIDDEN
* C-like expressions used in many places in linker scripts
* Input to output sections mapping

The goal of this commit was guided towards completely parsing a default GNU ld
linker script and the linker script used to link the FreeBSD kernel. Thus, it
also adds a test case based on the default linker script used in GNU ld for
x86_64 ELF targets. I tested SPEC userland programs linked by GNU ld, using the
linker script dump'ed by this parser, and everything went fine. I then tested
linking the FreeBSD kernel with a dump'ed linker script, installed the new
kernel and booted it, everything went fine.

Directives that still need to be implemented:
* MEMORY
* PHDRS

Reviewers: silvas, shankarke and ruiu

http://reviews.llvm.org/D5852

llvm-svn: 221126
2014-11-03 04:09:51 +00:00
Tim Northover cadb77a43e [mach-o] explicitly cast little_n uses to ints to silence MSVC
Hopefully this'll fix the build failure in the bot.

llvm-svn: 221007
2014-10-31 22:12:20 +00:00
Tim Northover 516e983dd4 [mach-o] use the addend in GOT-based relocations.
These are used to reference typeinfo data in the exception table section, for
example.

llvm-svn: 220916
2014-10-30 18:01:07 +00:00
Nick Kledzik 54ce295849 [mach-o] generate LC_DATA_IN_CODE in final linked images
lld was regenerating LC_DATA_IN_CODE in .o output files, but not into
final linked images.

Update test case to verify data-in-code info makes it into final linked images.

llvm-svn: 220827
2014-10-28 22:21:10 +00:00
Nick Kledzik cb955ae1a5 [mach-o] Turn -single_module error into a warning
To be more compatible with existing darwin linker.

llvm-svn: 220822
2014-10-28 21:11:02 +00:00
Tim Northover 40d3ad3319 TMP: fix readN & writeN to not encourage UB
llvm-svn: 220730
2014-10-27 22:48:35 +00:00
NAKAMURA Takumi 59c74b225a Fix unicode chars into ascii in comment lines.
llvm-svn: 220668
2014-10-27 08:08:18 +00:00
Rui Ueyama fccf7ef924 Remove #ifdef __FreeBSD
r220665 makes configure or CMake to set HAVE_CXXABI_H on FreeBSD 10.0.
We don't need this ugly ifdef condition anymore.

llvm-svn: 220666
2014-10-27 07:44:40 +00:00
Nick Kledzik 4183dbcc03 [mach-o] Support old style objc class names in export lists
Objective-C switched to a new ABI which uses a different mangling for class
names.  But to keep projects building that use export lists that use the old
class name mangling, the linker recognizes the old names and transforms them
to the new mangling.

llvm-svn: 220598
2014-10-24 22:28:54 +00:00
Nick Kledzik 761d6549bc [mach-o] Support data-only dylibs
In final linked shared images, the __TEXT segment contains both code and
the mach-o header/load-commands.  In the case of a data-only dylib, there is
no code, so we need to force the addition of the __TEXT segment.

llvm-svn: 220597
2014-10-24 22:19:22 +00:00
Rui Ueyama 875301b2c4 [PECOFF] Do not write duplicate directives to .def file.
This is a follow-up patch for r220333. r220333 renames exported symbols.
That raised another issue; if we have both decorated and undecorated names
for the same symbol, we'll end up have two duplicate exported symbol
entries.

This is a fix for that issue by removing duplciate entries.

llvm-svn: 220350
2014-10-22 00:05:30 +00:00
Nick Kledzik 9133f8c76d [mach-o] Support missing MH_SUBSECTIONS_VIA_SYMBOLS
All compiler generated mach-o object files are marked with MH_SUBSECTIONS_VIA_SYMBOLS.
But hand written assembly files need to opt-in if they are written correctly.
The flag means the linker can break up a sections at symbol addresses and
dead strip or re-order functions.

This change recognizes object files without the flag and marks its atoms as
not dead strippable and adds a layout-after chain of references so that the
atoms cannot be re-ordered.

llvm-svn: 220348
2014-10-21 23:45:37 +00:00
Rui Ueyama 8bae8189b2 [PECOFF] Fix exported symbol in the import library
There are two ways to specify a symbol to be exported in the module
definition file.

1) EXPORT <external name> = <symbol>
2) EXPORT <symbol>

In (1), you give both external name and internal name. In that case,
the linker tries to find a symbol using the internal name, and write
that address to the export table with the external name. Thus, from
the outer world, the symbol seems to be exported as the external name.

In (2), internal name is basically the same as the external name
with an exception: if you give an undecorated symbol to the EXPORT
directive, and if the linker finds a decorated symbol, the external
name for the symbol will become the decorated symbol.

LLD didn't implement that exception correctly. This patch fixes that.

llvm-svn: 220333
2014-10-21 21:41:28 +00:00
Nick Kledzik a721db24c0 Subclass InputGraph to get darwin linker library semantics
The darwin linker operates differently than the gnu linker with respect to
libraries. The darwin linker first links in all object files from the command
line, then to resolve any remaining undefines, it repeatedly iterates over
libraries on the command line until either all undefines are resolved or no
undefines were resolved in the last pass.

When Shankar made the InputGraph model, the plan for darwin was for the darwin
driver to place all libraries in a group at the end of the InputGraph. Thus
making the darwin model a subset of the gnu model. But it turns out that does
not work because the driver cannot tell if a file is an object or library until
it has been loaded, which happens later.

This solution is to subclass InputGraph for darwin and just iterate the graph
the way darwin linker needs.

llvm-svn: 220330
2014-10-21 21:14:11 +00:00
Rui Ueyama 1d6d08bb4d Use __cxa_demangle on FreeBSD to fix buildbots.
llvm-svn: 220326
2014-10-21 21:05:01 +00:00
Rui Ueyama e7bf4299db Assume cxxabi.h exists on FreeBSD
HAVE_CXXABI_H is not defined on FreeBSD but the system actually
has the header. CMake test fails because the header depends on size_t.

llvm-svn: 220315
2014-10-21 20:02:00 +00:00
Shankar Easwaran 1b44519a6c [Gnu] Move certain options to a different group.
The -mllvm option is not specific to LTO.

llvm-svn: 220263
2014-10-21 04:02:32 +00:00
Shankar Easwaran 322d0df309 [ELF] Remove duplicate code.
The base class ELFObjectReader/ELFDSOReader implement the canParse functionaity
with this change.

llvm-svn: 220261
2014-10-21 03:12:57 +00:00
Rui Ueyama f3208b808d [PECOFF] Look for decorated entry symbol name.
Entry symbol name can be decorated. When we look for _WinMain, we
also have to look for _WinMain@16.

llvm-svn: 220259
2014-10-21 02:28:28 +00:00
Shankar Easwaran 3dadd20fe5 [ELF] Implement demangle.
This adds functionality in the GNU flavor to demangle symbols when
undefined symbols are displayed to the user.

llvm-svn: 220184
2014-10-20 05:04:53 +00:00
Shankar Easwaran 1635f8de72 [ELF] Check for target architecture.
The canParse function for all the ELF subtargets check if the input files match
the subtarget.

There were few mismatches in the input files that didnt match the subtarget for
which the link was being invoked, which also acts as a test for this change.

llvm-svn: 220182
2014-10-20 04:00:26 +00:00
Shankar Easwaran 116ac7ce87 [ELF] Make updateReferenceForMergeStringAccess virtual.
The ELF subtargets would usually want to override the function
updateReferenceForMergeStringAccess. Allow this by making it virtual.

llvm-svn: 220180
2014-10-20 03:03:43 +00:00
Shankar Easwaran 6cfcb388b0 [ELF] Fix functionality of merging similar strings.
For PC relative accesses, negative addends were to be ignored. The linker was
not ignoring it and would fail with an assert. This fixes the issue and is able
to get Helloworld working.

llvm-svn: 220179
2014-10-20 02:59:06 +00:00
Shankar Easwaran 1175153867 [ELF][Cleanup] Remove unused code.
The old code was used as a workaround to fix how relocations are calculated for
sections with SHF_MERGE|SHF_STRINGS attribute. This patch removes the erroneous
code.

llvm-svn: 220159
2014-10-19 03:28:13 +00:00
Shankar Easwaran 69df16f619 Fix display of files processed by the linker.
This fixes the way archive members are displayed when the linker is used with a
flag to show all the files that it processes.

When an archive file member is read, we need to show the archive filename and
the member.

llvm-svn: 220144
2014-10-18 20:36:35 +00:00
Shankar Easwaran 2b67fca033 Sort include files according to convention.
llvm-svn: 220131
2014-10-18 05:33:55 +00:00
Shankar Easwaran ac23808619 [ELF] Add Readers for all the ELF subtargets.
This would permit the ELF reader to check the architecture that is being
selected by the linking process.

This patch also sorts the include files according to LLVM conventions.

llvm-svn: 220129
2014-10-18 05:23:17 +00:00
Richard Smith f83ba5eb0f Hardcode the list of ELF targets here rather than using a glob.
The code was making non-portable assumptions about the exact string returned by
the glob (possibly by the shell?); this is more robust and matches what is done
everywhere else.

llvm-svn: 220117
2014-10-18 00:56:31 +00:00
Tim Northover 24b7dd431e [mach-o] keep Windows library happy.
&v[v.size()] may be invalid C++.

llvm-svn: 220115
2014-10-18 00:36:03 +00:00
Rui Ueyama 2845fb29d8 [PECOFF] Emit x64 delay-import wrapper function
Previously we supported x86 only. This patch is to support x64.

The array of pointers to delay-loaded functions points the
DLL delay loading function at start-up. When a function is called
for the first time, the delay loading function gets called and
then rewrite the function pointer array.

llvm-svn: 220096
2014-10-17 22:03:54 +00:00
NAKAMURA Takumi a4dce82065 lld/lib/Core/Error.cpp: Appease g++-4.7, corresponding to LLVM r210687.
llvm-svn: 220039
2014-10-17 12:55:49 +00:00
NAKAMURA Takumi 8c38df4e9e [CMake] lld: Introduce ${cmake_2_8_12_INTERFACE} onto each target_link_libraries. [PR20254]
FIXME: Dependencies should be reorganized.
llvm-svn: 220000
2014-10-17 00:37:49 +00:00
Nick Kledzik 3a118aa344 [mach-o] Fix crash when -all_load used with dylibs
-all_load tells the darwin linker to immediately load all members of all
archives.  The code do that used reinterpret_cast<> instead of dyn_cast<>.
If the file was a dylib, the reinterpret_cast<> turned a pointer to a dylib
into a pointer to an archive...boom.

Added test case to reproduce the crash, simplified the code and used dyn_cast<>.

llvm-svn: 219990
2014-10-16 23:42:42 +00:00
Tim Northover 933bead97d [mach-o] update __eh_frame handling for Nick's suggestions
First, add a comment to support more variation in FDE formats. Second, refactor
fde -> function handling into a separate function living in the ArchHandler.

llvm-svn: 219959
2014-10-16 20:52:18 +00:00
Nick Kledzik 5172067391 [mach-o] Add support for upward linking
To deal with cycles in shared library dependencies, the darwin linker supports
marking specific link dependencies as "upward".  An upward link is when a
lower level library links against a higher level library.

llvm-svn: 219949
2014-10-16 19:31:28 +00:00
Rui Ueyama b38b96ab4c [PECOFF] Support delay-load import table for x86
This patch creates the import address table and sets its
address to the delay-load import table. This also creates
wrapper functions for __delayLoadHelper2.

x86 only for now.

llvm-svn: 219948
2014-10-16 19:30:44 +00:00
Tim Northover 995abe34f9 [mach-o] avoid overly clever std::find_if
The bots were complaining (possibly because of a lack of traits on the iterator
I was trying to use). No functional change.

llvm-svn: 219843
2014-10-15 20:26:24 +00:00
Tim Northover 1cc4fb76da [mach-o] make __unwind_info defer to __eh_frame when necessary.
Not all situations are representable in the compressed __unwind_info format,
and when this happens the entry needs to point to the more general __eh_frame
description.

Just x86_64 implementation for now.

rdar://problem/18208653

llvm-svn: 219836
2014-10-15 19:32:21 +00:00
Tim Northover a6a6ab9949 [macho] Create references from __eh_frame FDEs to their function.
We'll also need references back to the CIE eventually, but for now making sure
we can work out what an FDE is referring to is enough.

The actual kind of reference needs to be different between architectures,
probably because of MachO's chronic shortage of relocation types but I don't
really want to know in case I find out something that distresses me even more.

rdar://problem/18208653

llvm-svn: 219824
2014-10-15 18:19:31 +00:00
Rui Ueyama 0abf613059 Use isa<> and cast<> instead of definition().
No functionality change intended.

llvm-svn: 219727
2014-10-14 21:42:08 +00:00
Rui Ueyama 82399f96ae Remove dead code.
Because we use cast<> at the beginning of this function, it will
abort there if a given atom is not a DefinedAtom.

In the switch statement, we checked if a given atom is a DefinedAtom
again by evaluating definition() == Atom::definitionRegular.
This was always true. So we can remove the outer switch statement.

llvm-svn: 219724
2014-10-14 21:37:33 +00:00
Rui Ueyama e1ebc76072 Use isa<> instead of checking return value of definition().
definition() is supposed to be used through isa, dyn_cast or cast.
It's better to not call that directly.

llvm-svn: 219723
2014-10-14 21:20:01 +00:00
Rui Ueyama 4121d5a266 Use llvm::make_unique.
llvm-svn: 219709
2014-10-14 19:43:37 +00:00
Nick Kledzik 4121bce3f6 [mach-o] Add Pass to create are shim Atoms for ARM interworking.
Arm code has two instruction encodings "thumb" and "arm".  When branching from
one code encoding to another, you need to use an instruction that switches
the instruction mode.  Usually the transition only happens at call sites, and
the linker can transform a BL instruction in BLX (or vice versa).  But if the
compiler did a tail call optimization and a function ends with a branch (not
branch and link), there is no pc-rel BX instruction.

The ShimPass looks for pc-rel B instructions that will need to switch mode.
For those cases it synthesizes a shim which does the transition, then modifies
the original atom with the B instruction to target to the shim atom.

llvm-svn: 219655
2014-10-14 01:51:42 +00:00
Rafael Auler b722e31906 Reapply [ELF] Only mark as DT_NEEDED libs that are strictly necessary (r219353)
When committed in r219353, this patch originally caused problems because it was
not tested in debug build. In such scenarios, Driver.cpp adds two additional
passes. These passes serialize all atoms via YAML and reads it back. Since the
patch changed ObjectAtom to hold a new reference, the serialization was removing
the extra data.

This commit implements r219853 in another way, similar to the original MIPS way,
by using a StringSet that holds the names of all copied atoms instead of
directly holding a reference to the copied atom. In this way, this commit is
simpler and eliminate the necessity of changing the DefinedAtom hierarchy to
hold a new data.

Reviewers: shankarke

http://reviews.llvm.org/D5713

llvm-svn: 219449
2014-10-09 22:06:38 +00:00
Rui Ueyama 4ed77602a6 [PECOFF] Refactor _imp_ symbol generator.
We are going to have another type of jump table for the delay-load
import table. In order to prepare for that, we want to factor out
the function handling the jump table. No functionality change.

llvm-svn: 219446
2014-10-09 21:52:22 +00:00
Rui Ueyama a35a944952 [PECOFF] Remove another use of is64 to support non-Intel architecture
llvm-svn: 219438
2014-10-09 20:31:48 +00:00
Rui Ueyama 98d703a663 [PECOFF] Emit ModuleHandle field in delay-import table.
Previously the field was not set. The field should be pointing to
a placeholder where the DLL delay-loader writes the base address
of a DLL.

llvm-svn: 219415
2014-10-09 17:43:01 +00:00
Rui Ueyama 7be03d09cd [PECOFF] Emit the delay-import table
This is a partial patch to emit the delay-import table. With this,
LLD is now able to emit the table that llvm-readobj can read and
dump.

The table lacks a few fields, such as the address of HMODULE, the
import address table, etc. They'll be added in subsequent patches.

llvm-svn: 219384
2014-10-09 02:48:14 +00:00
Rafael Auler 9fe8db2e02 [ELF] Fix uninitialized variables
Properly initialize _exportDynamic in ELFLinkingContext and an ELF_Sym object
created in CRuntimeFile with default values.

llvm-svn: 219380
2014-10-09 01:54:15 +00:00
Nick Kledzik cb2018f57c [mach-o] Use default for min OS version when not specified on command line.
This matches the current darwin linker.

llvm-svn: 219376
2014-10-09 01:01:16 +00:00
Rui Ueyama b3f97ba815 Revert "[ELF] Only mark as DT_NEEDED libs that are strictly necessary"
This reverts commit r219353 because that seems to break buildbots.

llvm-svn: 219369
2014-10-09 00:05:55 +00:00
Rafael Auler 7a18a385f0 [ELF] Only mark as DT_NEEDED libs that are strictly necessary
Enhances the creation of an ELF dynamic executable by avoiding recording
unnecessary shared libraries as NEEDED to load a program.

To do this, we must keep track of not only symbols that were referenced but
also of COPY relocations, which steal the symbol from a shared library but does
not store from which lib this symbol came from. To fix this, this commit changes
ObjectSymbol to store the original library from which this symbol came. With
this information, we are able to build a list of the exact shared libraries that
must be marked as DT_NEEDED, instead of blindly marking all shared libraries as
needed.

This logic originally came from the MIPS backend with some adaptation.

Reviewers: atanasyan, shankar.easwaran

http://reviews.llvm.org/D5574

llvm-svn: 219353
2014-10-08 22:53:49 +00:00
Rafael Auler ca21009fb7 [ELF] [X86_64] Update TODO.rst list wrt r218633
Updates the remaining tasks in the X86_64 ELF lld backend after the commit that
handles general dynamic TLS relocations.

Reviewer: shankarke

http://reviews.llvm.org/D5673

llvm-svn: 219350
2014-10-08 22:40:43 +00:00
Rafael Auler 776293660e [ELF] Fix inclusion of weak symbols in the dynamic symbol table
This commit implements in the X86_64 ELF lld backend yet another feature that
was only available in the MIPS backend. However, this patch changes generic ELF
classes to make it trivial for other ELF backends to use this logic too. When
creating a dynamic executable that has dynamic relocations against weak
undefined symbols, these symbols must be exported to the dynamic symbol table
to seek a possible resolution at run time.

A common use case is the __gmon_start__ weak glibc undefined symbol.

Reviewer: shankarke

http://reviews.llvm.org/D5571

llvm-svn: 219349
2014-10-08 22:38:10 +00:00
Rui Ueyama aa5add859d [PECOFF] Simplify IdataPass. No functionality change.
llvm-svn: 219348
2014-10-08 22:33:00 +00:00
Rui Ueyama ecbb309e5e [PECOFF] Remember DLL names given with /delayload option.
This is a step toward full support of /delayload.

llvm-svn: 219344
2014-10-08 21:38:10 +00:00
Rui Ueyama 42466be42c Use llvm::StringSwitch.
llvm-svn: 219341
2014-10-08 21:08:15 +00:00
Rafael Auler ce1af1a201 [ELF] Implement --export-dynamic/-E
When creating a dynamic executable and receiving the -E flag, the linker should
export all globally visible symbols in its dynamic symbol table.

This commit also moves the logic that exports symbols in the dynamic symbol
table from OutputELFWriter to the ExecutableWriter class. It is not correct to
leave this at OutputELFWriter because DynamicLibraryWriter, another subclass of
OutputELFWriter, already exports all symbols, meaning we can potentially end up
with duplicated symbols in the dynamic symbol table when creating shared libs.

Reviewers: shankarke

http://reviews.llvm.org/D5585

llvm-svn: 219334
2014-10-08 18:54:26 +00:00
Rui Ueyama 0bc6d5f97d Fix typo in comment
llvm-svn: 219326
2014-10-08 17:50:15 +00:00
Shankar Easwaran 14fc1c0240 Revert "[ELF][AllArchs] Fix includes"
This reverts commit e137dd93e1291a2d2fa7f41c8f8bcdb59c8b3225.

llvm-svn: 219313
2014-10-08 15:23:22 +00:00
Shankar Easwaran 47d5818452 [ELF][AllArchs] Fix includes
llvm-svn: 219278
2014-10-08 04:12:08 +00:00
Shankar Easwaran c3550f9231 Add support to print version.
Summary: Add support in the universal driver to print the lld version and the
repository version.

Test Plan: A driver test is added

Reviewers: kledzik, ruiu

Reviewed By: ruiu

Subscribers: llvm-commits

Projects: #lld

Differential Revision: http://reviews.llvm.org/D5641

llvm-svn: 219277
2014-10-08 03:47:51 +00:00
Nick Kledzik 14b5d208cb [mach-o] Support fat archives
mach-o supports "fat" files which are a header/table-of-contents followed by a
concatenation of mach-o files (or archives of mach-o files) built for
different architectures.  Previously, the support for fat files was in the
MachOReader, but that only supported fat .o files and dylibs (not archives).

The fix is to put the fat handing into MachOFileNode.  That way any input file
kind (including archives) can be fat.  MachOFileNode selects the sub-range
of the fat file that matches the arch being linked and creates a MemoryBuffer
for just that subrange.

llvm-svn: 219268
2014-10-08 01:48:10 +00:00
Rui Ueyama f20143825c [PECOFF] Move a function out of a class
I'm going to use this function both for the import table and the
delay-import table.

llvm-svn: 219267
2014-10-08 01:48:08 +00:00
Rui Ueyama 8951520c23 [PECOFF] Remove yet another use of _is64
No functionality change intended.

llvm-svn: 219246
2014-10-07 22:20:21 +00:00
Rui Ueyama 8f0c803c26 [PECOFF] Infer machine type from input object file
If /machine option is omitted, the linker needs to infer that from
input object files. This patch implements that.

llvm-svn: 219180
2014-10-07 01:38:46 +00:00
Saleem Abdulrasool 6cfcc294fe PE/COFF: rename applyRelocations* family
Use x86 and x64 which is the canonical Microsoft vernacular for the targets.
Addresses post-commit review comments from Rui.

llvm-svn: 219179
2014-10-07 01:12:13 +00:00
Saleem Abdulrasool 3ff2064241 PE/COFF: add ARM NT reference kinds
Teach the reader about ARM NT relocation types.  Although the writer cannot yet
perform the actual application of these relocations, the reader can at least now
identify the relocation types.

llvm-svn: 219178
2014-10-07 01:12:08 +00:00
Rui Ueyama 6d930787a7 Do not use llvm_unreachable at reachable code.
These lines can be reachable if we give a broken or unsupported
input object file.

llvm-svn: 219176
2014-10-07 00:40:54 +00:00
Saleem Abdulrasool 6211740789 PE/COFF: add a check to ensure that we dont mix up architectures
Previously, we would not check the target machine type and the module (object)
machine type.  Add a check to ensure that we do not attempt to use an object
file with a different target architecture.

This change identified a couple of tests which were incorrectly mixing up
architecture types, using x86 input for a x64 target.  Adjust the tests
appropriately.  The renaming of the input and the architectures covers the
changes to the existing tests.

One significant change to the existing tests is that the newly added test input
for x64 uses the correct user label prefix for X64.

llvm-svn: 219093
2014-10-05 23:43:59 +00:00
Saleem Abdulrasool 1277faf54d Core: switch error mapping to switches
Rather than a series of cascading ifs, use a switch statement to convert the
error code to a string.  This has the benefit of allowing the compiler to inform
us if we ever add a new error code but fail to update the string representation.

Add in stringified versions for a couple of missing InputGraphErrors.

llvm-svn: 219089
2014-10-05 21:30:32 +00:00
Saleem Abdulrasool c2e691ed7c PE/COFF: remove another use of PECOFFLinkingContext::is64bit
In order to support more than x86/x86_64, we need to change the behaviour to use
the actual machine type rather than checking the bitness and assuming that we
are on X86.  This replaces the use of is64bit in applyAllRelocations with a
check on the machine type.  This will enable adding support for handling ARM
relocations.

Rename the existing applyRelocation methods to be similarly named and to make it
clear the types of relocations they will process.

llvm-svn: 219088
2014-10-05 21:30:29 +00:00
Nick Kledzik 15eba696f6 [mach-o] add -iphoneos_version_min as alias for -ios_version_min
llvm-svn: 219041
2014-10-04 00:19:56 +00:00
Nick Kledzik 09d00bb4d7 [mach-o] Add support for -dependency_info command line option
This option is added by Xcode when it runs the linker.  It produces a binary
file which contains the file the linker used.  Xcode uses the info to
dynamically update it dependency tracking.

To check the content of the binary file, the test case uses a python script
to dump the binary file as text which FileCheck can check.

llvm-svn: 219039
2014-10-04 00:16:13 +00:00
Rafael Auler 6fd0afa195 [ELF] Fix bug in ELFFile::createAtoms() that caused lld to mislink musl
When creating the graph edges of the atoms of an ELF file, special care must be
taken with atoms that represent weak symbols. They cannot be the target of any
Reference::kindLayoutAfter edge because they can be merged and point to other
code, screwing up the final layout of the atoms. ELFFile::createAtoms()
correctly handles this corner case. The problem is that createAtoms() assumed
that there can be no zero-sized weak symbols, which is not true. Consider:

my_weak_func1:
my_weak_func2:
my_weak_func3:
code

In this case, we have two zero-sized weak symbols, my_weak_func1 and
my_weak_func2, and one non-zero weak symbol my_weak_func3. createAtoms() would
correctly handle my_weak_func3, but not the first two symbols. This problem
happens in the musl C library when a zero-sized weak symbol is merged and
screws up the file layout. Since this musl code lives at the finalization hooks,
any C program linked with LLD and musl was correctly executing, but segfaulting
at the end.

Reviewers: shankarke

http://reviews.llvm.org/D5606

llvm-svn: 219034
2014-10-03 22:50:50 +00:00
Rui Ueyama 711471ea1e [PECOFF] Resolve __delayLoadHelper2 if /delayload is given
DLL delay importing is a feature to load a DLL lazily, instead of
at program start-up time.

If the feature is turned on with the /delayload flag, the linker
resolves the delay-load helper function. All function pointer table
entries for the DLL are initially pointing to the helper function.
When called, the function loads and resolves the DLL symbols using
dlopen-ish Windows system calls and then write the reuslts to the
function pointer table. The helper function is in "delayimp.lib".

Note that this feature is not completely implemented yet. LLD
also needs to emit the table that's consumed by the delay-load
helper function. That'll be done in another patch.

llvm-svn: 218943
2014-10-03 01:14:50 +00:00
Nick Kledzik 7efd054479 [mach-o] preserve custom section names on coalesable strings
llvm-svn: 218894
2014-10-02 17:27:20 +00:00
Nick Kledzik 32d0d09bf8 Preserve custom section names when coalescing.
The mergeByContent attribute on DefinedAtoms triggers the symbol table to
coalesce atoms with the exact same content. The problem is that atoms can also
have a required custom section. The coalescing should never change the custom
section of an atom.

The fix is to only consider to atoms to have the same content if their
sectionChoice() and customSectionName() attributes match.

llvm-svn: 218893
2014-10-02 17:22:05 +00:00
Rafael Auler 1d73ae03c9 [ELF] Never mark the dynamic linker as DT_NEEDED
This patch adds logic to avoid putting the dynamic linker library (ld.so) as a
DT_NEEDED entry in the dynamic table. It should only appear in PT_INTERP.

This patch fixes SPEC programs 433, 445, 450, 453, 456, 462 when running on
Ubuntu Linux x86_64 and when linking SPEC programs with LLD and glibc 2.19.

http://reviews.llvm.org/D5573

llvm-svn: 218847
2014-10-02 03:52:54 +00:00
Rafael Auler 8400fa06b5 [lld] [ELF] [MIPS] Remove duplicate logic
Summary: With r218633, the logic that monitors which shared library symbols were used was copied from the MIPS lld backend to ELF classes, making it available to all ELF backends. However, this made the isDynSymEntryRequired() functions in MipsDynamicLibraryWriter.h/MipsELFWriters.h/MipsExecutableWriter.h to be duplicated logic, since this is already implemented in OutputELFWriter<>/DefaultLayout. This patch removes this duplicated code from MIPS.

Reviewers: Bigcheese, shankarke

Reviewed By: shankarke

Subscribers: llvm-commits

Differential Revision: http://reviews.llvm.org/D5564

llvm-svn: 218846
2014-10-02 01:30:29 +00:00
Nick Kledzik 22c9073ada Add MachOLinkingContext parameter to MachOFileNode constructor.
No functionality change.  This removes a down-cast from LinkingContext to
MachOLinkingContext.

Also, remove const from LinkingContext::createImplicitFiles() to remove
the need for another const cast.  Seems reasonable for createImplicitFiles()
to need to modify the context (MachOLinkingContext does).

llvm-svn: 218796
2014-10-01 20:24:30 +00:00
Rui Ueyama 14798caac6 [PECOFF] Fix x64 export table.
Export table entry is 64 bit wide in x64. If MSB is 1, it means it's
imported by ordinal. The shift value was wrong.

llvm-svn: 218728
2014-10-01 01:39:34 +00:00
Nick Kledzik be43d7ef29 [mach-o] Implement -demangle.
The darwin linker has the -demangle option which directs it to demangle C++
(and soon Swift) mangled symbol names. Long term we need some Diagnostics object
for formatting errors and warnings. But for now we have the Core linker just
writing messages to llvm::errs(). So, to enable demangling, I changed the
Resolver to call a LinkingContext method on the symbol name.

To make this more interesting, the demangling code is done via __cxa_demangle()
which is part of the C++ ABI, which is only supported on some platforms, so I
had to conditionalize the code with the config generated HAVE_CXXABI_H.

llvm-svn: 218718
2014-09-30 23:15:39 +00:00
Rui Ueyama 07fae9691b [PECOFF] Fix /entry option.
This is yet another edge case of ambiguous name resolution.
When a symbol is specified with /entry:SYM, SYM may be resolved
to the C++ mangled function name (?SYM@@YAXXZ).

llvm-svn: 218706
2014-09-30 21:39:49 +00:00
Rui Ueyama dbddf11649 [PECOFF] Move helper function out of class
No functionality change intended.

llvm-svn: 218705
2014-09-30 21:39:46 +00:00
Tim Northover 3fc12bf860 [mach-o] add file comment to compact unwind pass
llvm-svn: 218704
2014-09-30 21:32:46 +00:00
Tim Northover cf78d37fd6 [mach-o] create __unwind_info section on x86_64
This is a minimally useful pass to construct the __unwind_info section in a
final object from the various __compact_unwind inputs. Currently it doesn't
produce any compressed pages, only works for x86_64 and will fail if any
function ends up without __compact_unwind.

rdar://problem/18208653

llvm-svn: 218703
2014-09-30 21:29:54 +00:00
Rui Ueyama fa67adc28d [PECOFF] Allow /export:<symbol>,PRTVATE.
PRIVATE option is also an undocumented feature.

llvm-svn: 218696
2014-09-30 20:09:31 +00:00
Rui Ueyama 3837e10002 [PECOFF] Fix /export option.
MSDN doesn't say about /export:foo=bar style option, but
it turned out MSVC link.exe actually accepts that. So we need that
too.

It also means that the export directive in the module definition
file and /export command line option are functionally equivalent.

llvm-svn: 218695
2014-09-30 20:03:11 +00:00
Rui Ueyama 3041443b5c [PECOFF] Fix __imp_ prefix on x64.
"__imp_" prefix always starts with double underscores.
When I was writing the original code I misunderstood
that it's "_imp_" on x64.

llvm-svn: 218690
2014-09-30 19:42:04 +00:00
Nick Kledzik b166d45730 [mach-o] Move implementation of MachOFileNode::parse
Move method implementation from header file to .cpp file.  No functionality
change.

llvm-svn: 218639
2014-09-29 23:04:24 +00:00
Rafael Auler 5a1000dddc [lld] [ELF] Support for general dynamic TLS relocations on X86_64
Summary:
This patch adds support for the general dynamic TLS access model for X86_64 (see www.akkadia.org/drepper/tls.pdf).

To properly support TLS, the patch also changes the __tls_get_addr atom to be a shared library atom instead of a regularly defined atom (the previous lld approach). This closely models the reality of a function that will be resolved at runtime by the dynamic linker and loader itself (ld.so). I was tempted to force LLD to link against ld.so itself to resolve these symbols, but since GNU ld does not need the ld.so library to resolve this symbol, I decided to mimic its behavior and keep hardwired a definition of __tls_get_addr in the lld code.

This patch also moves some important logic that previously was only available to the MIPS lld backend to be used to all ELF backends. This logic, which now lives in the DefaultLayout class, will monitor which external (shared lib) symbols are really imported by the current module and will only populate the dynamic symbol table with used symbols, as opposed to the previous approach of dumping all shared lib symbols in the dynamic symbol table. This is important to this patch to avoid __tls_get_addr from getting injected into all dynamic symbol tables.

By solving the previous problem of always adding __tls_get_addr, now the produced symbol tables are slightly smaller. But this impacted several tests that relied on hardwired/predefined sizes of the symbol table, requiring this patch to update such tests.

Test Plan: Added a LIT test case that exercises a simple use case of TLS variable in a shared library.

Reviewers: ruiu, rafael, Bigcheese, shankarke

Reviewed By: Bigcheese, shankarke

Subscribers: emaste, shankarke, joerg, kledzik, mcrosier, llvm-commits

Projects: #lld

Differential Revision: http://reviews.llvm.org/D5505

llvm-svn: 218633
2014-09-29 22:05:26 +00:00
Rui Ueyama d20d44fbe6 Use DenseMap::lookup. No functionality change.
llvm-svn: 218554
2014-09-26 23:21:10 +00:00
Rui Ueyama b774a0e750 Fix crash bug on Windows.
Mutating the DenseMap here seems to cause the Windows executable
to crash. Don't use operator[] to access possibly nonexistent key.

llvm-svn: 218548
2014-09-26 22:27:42 +00:00
Rui Ueyama 1b0e68353d [PECOFF] Fix module definition file output
Previously we emit two or more identical definitions for an
exported symbol if the same /export option is given more than
once. This patch fixes that bug.

llvm-svn: 218433
2014-09-25 00:52:38 +00:00
Chad Rosier 073cbd47f3 [AArch64] Fix an incorrect PLT entry.
This patch is difficult to test in isolation, so a subsequent patch will test
further.

Patch by Daniel Stewart <stewartd@codeaurora.org>!
Phabricator Revision: http://reviews.llvm.org/D5377

llvm-svn: 218418
2014-09-24 21:52:31 +00:00
Rui Ueyama 1c244f5db3 [PECOFF] Do not print @<ordinal> if the symbol is private
lib.exe prints a warning if a symbol in a module definition file has
both the PRIVATE attribute and an ordinal like this.

  EXPORTS
    foo @1 PRIVATE

This patch suppresses that.

llvm-svn: 218395
2014-09-24 17:51:51 +00:00
Rui Ueyama a370c3ee35 [PECOFF] Exported name should match C++ mangled name
Currently you can omit the leading underscore from exported
symbol name. LLD will look for mangled name for you. But it won't
look for C++ mangled name.

This patch is to support that.

If "sym" is specified to be exported, the linker looks for not
only "sym", but also "_sym" and "?sym@@<whatever>", so that you
can export a C++ function without decorating it.

llvm-svn: 218355
2014-09-24 02:01:10 +00:00
Rui Ueyama 359a47cee6 [PECOFF] Keep renamed undefined symbol name in export descriptor
Exported symbol name resolution is two-pass. In the first pass,
we try to resolve that as a regular undefined symbol. If it fails,
we look for mangled name for the symbol and rename the undefined
symbol and try again.

After all name resolution is done, we look for an atom for each
exported symbol again, to construct the export table. In this
process we try the regular names first, and then try mangled names.
But at this moment we should have knew which name is correct.

This patch is to keep the information we get in the first process
to use it later.

llvm-svn: 218354
2014-09-24 01:44:59 +00:00
Rui Ueyama de18f9657b [PECOFF] Keep unmangled name in the export table descriptor
The export table descriptor is a data structure to keep information
about the export table. It contains a symbol name, and the name may
or may not be mangled.

We need unmangled names for the export table, so we demangle them
before writing them to the export table.

Obviously this is not a correct round-trip conversion. That could
drop a leading underscore from a symbol because that's
indistinguishable from a mangled name.

What we need to do is to keep unmangled names. This patch does that.

llvm-svn: 218345
2014-09-24 00:55:15 +00:00
Rui Ueyama 251d9a34e2 [PECOFF] Simplify /machine option handling
/machine:ebc was previously recognized but rejected. Unknown architecture
names were handled differently but eventually rejected too. We don't need
to distinguish them.

llvm-svn: 218344
2014-09-24 00:21:45 +00:00
Rui Ueyama 75c0127bb3 [PECOFF] Change export table type.
This patch changes the type of export table set from std::set to
std::vector. The new code is slightly inefficient, but because
export table elements are actually mutable, std::vector is better
here. No functionality change.

llvm-svn: 218343
2014-09-24 00:09:36 +00:00
Rui Ueyama 7ea46bab26 [PECOFF] Fix duplicate /export options
If two or more /export options are given for the same symbol, we should
always print a warning message and use the first one regardless of other
parameters.
Previously there was a case that the first parameter is not used.

llvm-svn: 218342
2014-09-23 23:49:41 +00:00
Rui Ueyama a3eff3afd9 Make -core/-flavor options have higher priority than linker name
Also allows -core/flavor to appear at any position in the command line.
Patch from Oleg Ranevskyy!

http://reviews.llvm.org/D5384

llvm-svn: 218321
2014-09-23 18:09:38 +00:00
Rui Ueyama 117ef70c98 [PECOFF] Handle PRIVATE keyword in the module definition file
A symbol in a module definition file may be annotated with the
PRIVATE keyword like this.

  EXPORTS
    func PRIVATE

The PRIVATE keyword does not affect the resulting .dll file.
But it prevents the symbol to be listed in the .lib (import
library) file.

llvm-svn: 218273
2014-09-22 20:50:46 +00:00
Rui Ueyama 45f4d54c07 Re-commit r218259.
llvm-svn: 218272
2014-09-22 20:48:04 +00:00
Rui Ueyama 869c0019b1 Revert "[ELF] Fix linking when a regular object defines a symbol that is used in a DSO"
This commit reverts r218259 because it needed to be checked in with
a few binary files for the test.

llvm-svn: 218262
2014-09-22 18:08:34 +00:00
Rui Ueyama 508a007ae6 [ELF] Fix linking when a regular object defines a symbol that is used in a DSO
Patch from Rafael Auler!

When a shared lib has an undefined symbol that is defined in a regular object
(the program), the final executable must export this symbol in the dynamic
symbol table. However, in the current logic, lld only puts the symbol in the
dynamic symbol table if the symbol is weak. This patch fixes lld to put the
symbol in the dynamic symbol table regardless if it is weak or not.

This caused a problem in FreeBSD10, whose programs link against a crt1.o
that defines the symbol __progname, which is, in turn, undefined in libc.so.7
and will only be resolved in runtime.

http://reviews.llvm.org/D5424

llvm-svn: 218259
2014-09-22 17:52:50 +00:00
Yaron Keren 9682c8553e Modified per David Blakie suggestion.
llvm-svn: 218198
2014-09-21 05:07:44 +00:00
Yaron Keren 84f3816f85 Silence these C4715 warnings from Visual C++ (NFC)
llvm\tools\lld\lib\readerwriter\macho\macholinkingcontext.cpp(647):
warning C4715: 'lld::MachOLinkingContext::exportSymbolNamed' :
not all control paths return a value

llvm\tools\lld\lib\readerwriter\macho\machonormalizedfilefromatoms.cpp(723):
warning C4715: '`anonymous namespace'::Util::getSymbolTableRegion' :
not all control paths return a value

While all enum values do appear in the switch, an uninitialized or corrupted
enum variable would not be caught without the default: case in the switch.

llvm-svn: 218197
2014-09-21 04:13:45 +00:00
Rui Ueyama 44a7c7f1aa [PECOFF] Set ordinal to alias atoms
Atoms are ordered in the output file by ordinal. File has file ordinal,
and atom has atom ordinal which is unique within the file.
No two atoms should have the same combination of ordinals.

However that contract was not satisifed for alias atoms. Alias atom
is defined by /alternatename:sym1=sym2. In this case sym1 is defined
as an alias for sym2. sym1 always got ordinal 0.

As a result LLD failed with an assertion failure.

This patch assigns ordinal to alias atoms.

llvm-svn: 218158
2014-09-19 21:58:54 +00:00
Saleem Abdulrasool 752c9cb12f PECOFF: loosen another assumption of x86 only
Cache the machine type value of the linking context.  We need this in order to
calculate the virtual address of the atom when resolving function symbols.
Windows on ARM must check if the atom is a function and if so, set the Thumb bit
for the returned virtual address.  Failure to do so will result in an abnormal
exit due to a trap caused by invalid instruction decoding.  The same information
can be used to determine the relocation type that was previously being done via
is64 to select between x86 and x86_64.

llvm-svn: 218106
2014-09-19 06:09:33 +00:00
Saleem Abdulrasool c2c2937d55 Driver: accept /machine:arm for Windows linker
Accept /machine:arm as an argument.  This is changed to support ARM NT.
Although there is no way to differentiate between ARM (Windows CE) and ARM NT
(Windows on ARM), since LLVM currently only supports Windows on ARM, simply take
/machine:arm to mean Windows on ARM.

llvm-svn: 218105
2014-09-19 06:09:30 +00:00
Saleem Abdulrasool b9e9e0d5f3 PECOFF: loosen assumptions about x86-only targets
Rather than saving whether we are targeting 64-bit x86 (x86_64), simply convert
the single use of that information to the actual relocation type.  This will
permit the selection of non-x86 relocation types (e.g. for WoA support).

Inline the access of the machine type field as it is relatively cheap (a couple
of pointer dereferences) rather than storing the relocation type as a member
variable.

llvm-svn: 218104
2014-09-19 06:09:25 +00:00
Saleem Abdulrasool 42c7aab748 ReaderWriter: print magic in hex
When we encounter an unknown machine type, we print out the machine type magic.
However, we would print out the magic in decimal rather than hex.  Perform this
conversion to make it easier to identify what machine is unsupported.

llvm-svn: 218103
2014-09-19 06:09:18 +00:00
Rui Ueyama 68085fda00 [PECOFF] __tls_used is _tls_used on x64.
llvm-svn: 218090
2014-09-19 00:22:22 +00:00
Rui Ueyama 2ea8639696 Fixes wrong Twine uses in FileNode::errStr() and in LayoutPass.cpp
Patch from Rafael Auler!

llvm-svn: 218088
2014-09-18 23:21:39 +00:00
Rui Ueyama 1f684518c8 Fix buggy Twine storage in ELFLinkingContext::searchLibrary()
This patch fixes a forbidden use of Twine. It should only be used
as an intermediary value, but never stored.

This caused a bug in lld when running on Linux and compiled with
optimizations - it couldn't properly search libs.

Patch from Rafael Auler!

llvm-svn: 218083
2014-09-18 22:05:37 +00:00
Rui Ueyama 6bf091c656 [PECOFF] /safeseh:no on x64 is not an error
I made LLD to report an error if /safeseh:no option is given on x64,
but it turned out MSVC link.exe doesn't report error on it.
Removing the check.

llvm-svn: 218077
2014-09-18 21:18:05 +00:00
Rui Ueyama 9f1215b2d8 [PECOFF] Support TLS callbacks.
The contents from section .CRT$XLA to .CRT$XLZ is an array of function
pointers. They are called by the runtime when a new thread is created
or (gracefully) terminated.

You can make your own initialization function to be called by that
mechanism. All you have to do is:

- Define a pointer to a function in a .CRT$XL* section using pragma
- Make an external reference to "__tls_used" symbol

That technique is used in many projects. This patch is to support that.

What this patch does is to set the relative virtual address of
"__tls_used" to the PECOFF directory table. __tls_used is actually a
struct containing pointers to a symbol in .CRT$XLA and another symbol
in .CRT$XLZ. The runtime looks at the directory table, gets the address
of the struct, and call the function pointers between XLA and XLZ.

llvm-svn: 218007
2014-09-18 02:02:52 +00:00
Nick Kledzik 1050b57a0b [mach-o] Fix two-level namespace ordinals
On darwin, the linker tools records which dylib (DSO) each undefined was found
in, and then at runtime, the loader (dyld) only looks in that one specific
dylib for each undefined symbol.  Now that llvm-objdump can display that info
I can write test cases.

llvm-svn: 217898
2014-09-16 20:27:28 +00:00
David Majnemer 3588c6b198 Adjust lld to handle LLVM r217812
llvm-svn: 217815
2014-09-15 19:54:53 +00:00
Rui Ueyama f47c7fab71 Make anonymous namespace as small as possible.
LLVM coding style says that "static" is preferred for file-scope
functions.

Differential Revision: http://reviews.llvm.org/D5323

llvm-svn: 217692
2014-09-12 17:30:13 +00:00