Commit Graph

346 Commits

Author SHA1 Message Date
Simon Atanasyan 07000878f8 [ELF] Export strong defined symbol if it coalesces away a weak symbol
defined in a shared library.

Now LLD does not export a strong defined symbol if it coalesces away a
weak symbol defined in a shared library. This bug affects all ELF
architectures and leads to segfault:

  % cat foo.c
  extern int __attribute__((weak)) flag;
  int foo() { return flag; }

  % cat main.c
  int flag = 1;
  int foo();
  int main() { return foo() == 1 ? 0 : -1; }

  % clang -c -fPIC foo.c main.c
  % lld -flavor gnu -target x86_64 -shared -o libfoo.so ... foo.o
  % lld -flavor gnu -target x86_64 -o a.out ... main.o libfoo.so
  % ./a.out
  Segmentation fault

The problem is caused by the fact that we lose all information about
coalesced symbols after the `Resolver::resolve()` method is finished.

The patch solves the problem by overriding the
`LinkingContext::notifySymbolTableCoalesce()` method and saving names
of coalesced symbols. Later in the `buildDynamicSymbolTable()` routine
we use this information to export these symbols.

llvm-svn: 217363
2014-09-08 09:43:38 +00:00
Rui Ueyama 930bc70b75 Remove dead code.
llvm-svn: 217324
2014-09-07 01:23:16 +00:00
Rui Ueyama 839fb2f10c Simplify. No functionality changes.
llvm-svn: 216257
2014-08-22 02:00:58 +00:00
Nick Kledzik bb38f7bdaa Add notifier hooks to symbol table.
This is the one interesting aspect from:
   http://reviews.llvm.org/D4965

These hooks are useful for flavor specific processing, such as recording that
a DefinedAtom replaced a weak SharedLibraryAtom.

llvm-svn: 216122
2014-08-20 20:46:28 +00:00
Rui Ueyama abea8fa61d Change the signature of insertElementAt and rename addInputElementFront
insertElementAt(x, END) does the identical thing as addInputElement(x),
so the only reasonable use of insertElementAt is to call it with the
other possible argument, BEGIN. That means the second parameter of the
function is just redundant. This patch is to remove the second
parameter and rename the function accordingly.

llvm-svn: 213821
2014-07-24 00:08:22 +00:00
Rui Ueyama 9ec7e659d8 Remove all uses of llvm::function_ref from LLD.
llvm-svn: 213313
2014-07-17 21:33:21 +00:00
Rui Ueyama d5ba2a6994 Use std::function instead of llvm::function_ref.
llvm-svn: 213312
2014-07-17 21:23:52 +00:00
Rafael Espindola c2199ecf1e Update for llvm api change.
llvm-svn: 212407
2014-07-06 17:43:22 +00:00
Rui Ueyama 61d7f97000 [PECOFF] Support COMDAT associative sections.
COFF supports a feature similar to ELF's section groups. This
patch implements it.

In ELF, section groups are identified by their names, and they are
treated somewhat differently from regular symbols. In COFF, the
feature is realized in a more straightforward way. A section can
have an annotation saying "if Nth section is linked, link this
section too."

I added a new reference type, kindAssociate. If a target atom is
coalesced away, the referring atom is removed by Resolver, so that
they are treated as a group.

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

llvm-svn: 211106
2014-06-17 16:19:33 +00:00
Rafael Espindola b34440f9c4 Remove unused and odd code.
This code was never being used and any use of it would look fairly strange.
For example, it would try to map a NativeReaderError::file_malformed to
std::errc::invalid_argument.

llvm-svn: 210913
2014-06-13 15:36:45 +00:00
Rafael Espindola b1a4d3a26c Don't import error_code into the lld namespace.
llvm-svn: 210785
2014-06-12 14:53:47 +00:00
Rafael Espindola 4956850fdc replace llvm::error_code with std::error_code.
llvm-svn: 210781
2014-06-12 14:04:54 +00:00
Rafael Espindola 92d8b456d9 Use error_category from the std namespace.
llvm-svn: 210732
2014-06-12 01:44:19 +00:00
Rafael Espindola 7cc0d413b5 Use error_condition from the std namespace.
llvm-svn: 210730
2014-06-12 01:28:12 +00:00
Rui Ueyama e05d380486 Move Simple.h and Alias.h to include/Core.
Because the files in Core actually depend on these files.

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

llvm-svn: 210710
2014-06-11 21:47:51 +00:00
Rafael Espindola 7b8b9ae1cb Use std::error_code instead of llvm::error_code.
This is an update for a llvm api change.

llvm-svn: 210689
2014-06-11 19:05:58 +00:00
Rafael Espindola 1f7701c19c Missed these from the previous commit.
llvm-svn: 210629
2014-06-11 03:35:50 +00:00
Rafael Espindola 4b4cdf5e3a Inherit from error_category instead of _do_message.
There is no std::_do_message and these classes override message anyway.

llvm-svn: 210626
2014-06-11 03:28:13 +00:00
Rafael Espindola 3a3532ebb9 Mark a few functions noexcept.
llvm-svn: 210590
2014-06-10 21:26:18 +00:00
Rui Ueyama 733b45f3b0 Add SymbolTable::isCoalescedAway
isCoalescedAway(x) is faster than replacement(x) != x as the former
does not follow the replacement atom chain. Also it's easier to use.

llvm-svn: 210242
2014-06-05 07:37:29 +00:00
Rui Ueyama 52edc49031 Print error message in LinkOnce handler.
Rather than outside of the handler function to make the code simple.

llvm-svn: 210241
2014-06-05 07:37:25 +00:00
Iain Sandoe c4d9bedcfc Initial set of Makefiles
This provides support for the autoconfing & make build style.
The format, style and implementation follows that used within the llvm and clang projects.

TODO: implement out-of-source documentation builds.
llvm-svn: 210177
2014-06-04 09:54:07 +00:00
Rui Ueyama 6668b5a8b8 Fix build breakage.
llvm-svn: 210087
2014-06-03 07:53:37 +00:00
Rui Ueyama 6848f96c7a Inline short member function.
llvm-svn: 210086
2014-06-03 07:52:42 +00:00
Rui Ueyama 9aee050a0c Remove group-parent references.
Previously section groups are doubly linked to their children.
That is, an atom representing a group has group-child references
to its group contents, and content atoms also have group-parent
references to the group atom. That relationship was invariant;
if X has a group-child edge to Y, Y must have a group-parent
edge to X.

However we were not using group-parent references at all. The
resolver only needs group-child edges.

This patch simplifies the section group by removing the unused
reverse edge. No functionality change intended.

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

llvm-svn: 210066
2014-06-03 03:07:49 +00:00
Rui Ueyama 23487e878b Make dead-striping to handle reverse edges.
Layout-before edges are no longer used for layout, but they are
still there for dead-stripping. If we would just remove them
from code, LLD would wrongly remove live atoms that were
referenced by layout-befores.

This patch fixes the issue. Before dead-stripping, it scans all
atoms to construct a reverse map for layout-after edges. Dead-
stripping pass uses the map to traverse the graph.

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

llvm-svn: 210057
2014-06-03 01:59:02 +00:00
Rui Ueyama 6ffe42eee5 Simplify markLive().
Reference::target() never returns a nullptr, so NULL check
is not needed and is more harmful than doing nothing.
No functionality change.

llvm-svn: 210008
2014-06-02 08:06:57 +00:00
Rafael Espindola a72f21d195 There is no std::errc:success, remove the llvm one.
llvm-svn: 209958
2014-05-31 03:20:37 +00:00
Rafael Espindola 63ed1a3519 Use error_code() instead of error_code::succes()
There is no std::error_code::success, so this removes much of the noise
in transitioning to std::error_code.

llvm-svn: 209948
2014-05-31 01:22:21 +00:00
Nick Kledzik 388f3d02de [mach-o] Support parsing of non-lazy-pointer sections
llvm-svn: 209704
2014-05-28 01:16:35 +00:00
Nick Kledzik 181ce5ee03 Add make_dynamic_error_code().
This is a short-term fix to allow lld Readers to return error messages
with dynamic content.  

The long term fix will be to enhance ErrorOr<> to work with errors other
than error_code.  Or to change the interface to Readers to pass down a
diagnostics object through which all error messages are written.

llvm-svn: 209681
2014-05-27 19:35:41 +00:00
Rui Ueyama ba46cdb21f Move alias symbols from ELFLinkingContext to LinkingContext.
Alias symbols are SimpleDefinedAtoms and are platform neutral. They
don't have to belong ELF. This patch is to make it available to all
platforms. No functionality change intended.

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

llvm-svn: 209475
2014-05-22 21:37:56 +00:00
Rui Ueyama 328da5527e Simplify nullptr check.
llvm-svn: 209357
2014-05-22 00:36:36 +00:00
Rui Ueyama 2a52251153 Fix regression introduced in r205566.
In r205566, I made a change to Resolver so that Resolver revisit
only archive files in --start-group and --end-group pair. That's
not correct, as it also has to revisit DSO files.

This patch is to fix the issue.

Added a test to demonstrate the fix. I confirmed that it succeeded
before r205566, failed after r205566, and is ok with this patch.

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

llvm-svn: 208797
2014-05-14 17:29:27 +00:00
Rui Ueyama 551aacd6bc Add observers to Input Graph
Make it possible to add observers to an Input Graph, so that files
returned from an Input Graph can be examined before they are
passed to Resolver.

To implement some PE/COFF features we need to know all the symbols
that *can* be solved, including ones in archive files that are not
yet to be read.

Currently, Resolver only maintains a set of symbols that are
already read. It has no knowledge on symbols in skipped files in
an archive file.

There are many ways to implement that. I chose to apply the
observer pattern here because it seems most non-intrusive. We don't
want to mess up Resolver with architecture specific features.
Even in PE/COFF, the feature that needs this mechanism is minor.
So I chose not to modify Resolver, but add a hook to Input Graph.

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

llvm-svn: 208753
2014-05-14 05:31:54 +00:00
Rui Ueyama c7c47681f6 Revert "temporary commit."
This reverts accidental commit r208427.

llvm-svn: 208433
2014-05-09 16:47:20 +00:00
Rui Ueyama 4e0b17dd3c temporary commit.
llvm-svn: 208427
2014-05-09 16:35:23 +00:00
Rui Ueyama 9967f49a3c Simplify InputGraph::getNextFile. No functionality change.
llvm-svn: 208256
2014-05-07 22:27:12 +00:00
Rui Ueyama 3608609339 Expand nested input elements.
Previously only the toplevel elements were expanded by expandElements().
Now we recursively call getReplacements() to expand input elements even
if they are in, say, in a group.

llvm-svn: 208144
2014-05-06 23:52:19 +00:00
Rui Ueyama 1fe682cf37 Don't return value rather than always returning true.
addInputElement() never fails, and no one checks its return value
except tests. Let's simplify the signature.

llvm-svn: 208109
2014-05-06 17:33:14 +00:00
Rui Ueyama 20822d924e Return the expression result directly.
llvm-svn: 207878
2014-05-02 22:32:01 +00:00
Rui Ueyama 19173a39d3 Don't use "auto" where the actual type is not obvious.
llvm-svn: 207875
2014-05-02 21:48:01 +00:00
Rui Ueyama 331f482cf6 Split a utility function not to use goto statement.
llvm-svn: 205643
2014-04-04 18:34:40 +00:00
Rui Ueyama e8af3e48fd useNew is set to false in all branches, so set it to false outside the if-else.
llvm-svn: 205642
2014-04-04 18:21:53 +00:00
Rui Ueyama 01cc718375 Replace dyn_cast<T>s immediately followed by asserts with cast<T>s.
llvm-svn: 205641
2014-04-04 18:21:51 +00:00
Rui Ueyama fb7936d07c Fix indentation.
llvm-svn: 205639
2014-04-04 18:12:27 +00:00
Rui Ueyama fbd6c44e6a Revert "temporary commit."
This reverts commit r205635 that was submitted by mistake.

llvm-svn: 205637
2014-04-04 18:06:56 +00:00
Rui Ueyama 770305804f temporary commit.
llvm-svn: 205635
2014-04-04 18:01:52 +00:00
Rui Ueyama 992fdc0785 SymbolTable::size() returns an unsigned int.
It's better to use the same type rather than a fixed width integer type
that may be different from the return type.

llvm-svn: 205597
2014-04-04 01:22:51 +00:00
Rui Ueyama 7cac0f784c Use range-based for loop. No functionality change.
llvm-svn: 205594
2014-04-04 00:59:50 +00:00
Rui Ueyama 0b8e053ebd Do not use temporary variables to pass them to forEachUndefines.
So that it's obvious that we pass these callbacks only to forEachUndefines.

llvm-svn: 205593
2014-04-04 00:39:37 +00:00
Rui Ueyama 8dc9f0a371 Return a vector rather than mutating a given one.
This is cleaner and as efficient as before.

Differential Revision: http://llvm-reviews.chandlerc.com/D3284

llvm-svn: 205590
2014-04-04 00:15:52 +00:00
Rui Ueyama 8bd093b1e5 Rename getInputGraph() and getNextFile().
Seems getSomething() is more common naming scheme than just a noun
to get something, so renaming these members.

Differential Revision: http://llvm-reviews.chandlerc.com/D3285

llvm-svn: 205589
2014-04-04 00:14:04 +00:00
Rui Ueyama 1c3486a312 Update comment.
llvm-svn: 205579
2014-04-03 22:58:41 +00:00
Rui Ueyama 71c0202dd0 Minor cleanups.
llvm-svn: 205578
2014-04-03 22:43:42 +00:00
Rui Ueyama 4f010d2f66 Replace a recursion with a loop for speed.
llvm-svn: 205576
2014-04-03 22:36:55 +00:00
Rui Ueyama 450d987e3a Do not check deadStripNever twice.
Atoms with deadStripNever attribute has already been added to the
dead strip root set at end of Resolver::doDefinedAtom, so no need
to check it for each atom again.

Differential Revision: http://llvm-reviews.chandlerc.com/D3282

llvm-svn: 205575
2014-04-03 22:24:40 +00:00
Rui Ueyama 70625fb163 Move code into a helper function.
Move code that always runs after doUndefinedAtom into doUndefinedAtom
for readability.

llvm-svn: 205574
2014-04-03 22:21:59 +00:00
Rui Ueyama 517f0d9b5f Minor cleanup.
llvm-svn: 205570
2014-04-03 21:16:37 +00:00
Rui Ueyama 83e6acc7f2 Simplify two if's.
llvm-svn: 205569
2014-04-03 21:11:22 +00:00
Rui Ueyama fb44f5e278 Fix comments.
llvm-svn: 205568
2014-04-03 21:06:23 +00:00
Rui Ueyama 6aa91cf13d Early return.
llvm-svn: 205567
2014-04-03 21:00:03 +00:00
Rui Ueyama 9457510abf Fix ELFFileNode::resetNextIndex().
ELFLinkingContext has a method addUndefinedAtomsFromSharedLibrary().
The method is being used to skip a shared library within --start-group
and --end-group if it's not the first iteration of the group.

We have the same, incomplete mechanism to skip a shared library within
a group too. That's implemented in ELFFileNode. It's intended to not
return a shared library on the second or further iterations in the
first place. This mechanism is preferred over
addUndefinedAtomsFromSharedLibrary because the policy is implemented
in Input Graph -- that's what Input Graph is for.

This patch removes the dupluicate feature and fixes ELFFileNode.

Differential Revision: http://llvm-reviews.chandlerc.com/D3280

llvm-svn: 205566
2014-04-03 20:54:47 +00:00
Rui Ueyama 69fcde8b0a Expand 'auto' that's hard for human to deduce its real type.
llvm-svn: 205564
2014-04-03 20:47:50 +00:00
Rui Ueyama 4469ce6c7b Add empty() to atom_collection.
"x.empty()" is more idiomatic than "x.size() == 0". This patch is to
add such method and use it in LLD.

Differential Revision: http://llvm-reviews.chandlerc.com/D3279

llvm-svn: 205558
2014-04-03 19:51:14 +00:00
Rui Ueyama 2b2ac91fab Remove "this->".
For most member function calls we do not use "this->" in this file.
Remove the rest for consistency.

llvm-svn: 205550
2014-04-03 18:13:14 +00:00
Rui Ueyama b0e3b66fa5 Remove ordinals from Input Graph elements.
An ordinal is set to each child of Input Graph, but no one actually
uses it. The only piece of code that gets ordinaly values is
sortInputElements in InputGraph.cpp, but it does not actually do
anything -- we assign ordinals in increasing order just before
calling sort, so when sort is called it's already sorted. It's no-op.
We can simply remove it. No functionality change.

Differential Revision: http://llvm-reviews.chandlerc.com/D3270

llvm-svn: 205501
2014-04-03 02:21:44 +00:00
Rui Ueyama edd5c0a9ea Simplify communication between Resolver and Input Graph.
Resolver is sending too much information to Input Graph than Input
Graph actually needs. In order to collect the detailed information,
which wouldn't be consumed by anyone, we have a good amount of code
in Resolver, Input Graph and Input Elements. This patch is to
simplify it. No functionality change.

Specifically, this patch replaces ResolverState enum with a boolean
value. The enum defines many bits to notify the progress about
linking to Input Graph using bit masks, however, what Input Graph
actually does is to compare a given value with 0. The details of
the bit mask is simply being ignored, so the efforts to collect
such data is wasted.

This patch also changes the name of the notification interface from
setResolverState to notifyProgress, to make it sounds more like
message passing style. It's not a setter but something to notify of
an update, so the new name should be more appropriate than before.

Differential Revision: http://llvm-reviews.chandlerc.com/D3267

llvm-svn: 205463
2014-04-02 21:02:44 +00:00
Rui Ueyama 0f58c7cc3a Remove dead code.
llvm-svn: 205454
2014-04-02 19:42:35 +00:00
Rui Ueyama b461b1c785 s/llvm::dyn_cast/dyn_cast/
llvm-svn: 205404
2014-04-02 06:54:46 +00:00
Rui Ueyama 0ca88e3863 Use cast<T> instead of dyn_cast<T>.
llvm-svn: 205403
2014-04-02 06:54:43 +00:00
Rui Ueyama 43a589cc82 Remove dead code.
llvm-svn: 205401
2014-04-02 06:38:46 +00:00
Rui Ueyama 1ecad2c993 Remove dead code.
llvm-svn: 205397
2014-04-02 05:57:19 +00:00
Rui Ueyama eb4b54349d Move nextFile() from LinkingContext to InputGraph.
LinkingContext and InputGraph are unnecessarily entangled. Most linker
input file data, e.g. the vector containing input files, the next index
of the input file, etc. are managed by InputGraph, but only the current
input file is for no obvious reason managed by LinkingContext.

This patch is to move code from LinkingContext to InputGraph to fix it.
It's now clear who's reponsible for managing input file state, which is
InputGraph, and LinkingContext is now free from that responsibility.
It improves the readability as we now have fewer dependencies between
classes. No functionality change.

Differential Revision: http://llvm-reviews.chandlerc.com/D3259

llvm-svn: 205394
2014-04-02 05:03:40 +00:00
Rui Ueyama 5b8be49fed Remove unused enum Position::ANY and third parameter of insertElementAt().
insertElementAt()'s third parameter is not only unused but also ignored
if you pass Position::END. The actual meaning of the parameter was obscure.

Differential Revision: http://llvm-reviews.chandlerc.com/D3256

llvm-svn: 205376
2014-04-01 23:58:17 +00:00
Rui Ueyama ce749af25b Rename insertOneElementAt -> insertElementAt.
insertElementsAt() is removed, so "One" in insertOneElementAt() no longer
make much sense. Rename it for brevity.

llvm-svn: 205372
2014-04-01 23:17:28 +00:00
Rui Ueyama 26eb6c5bc2 Inline empty constructor.
llvm-svn: 205366
2014-04-01 22:11:18 +00:00
Rui Ueyama 5632e26d36 Greatly simplify InputGraph.
InputGraph has too many knobs and controls that are not being used. This
patch is to remove dead code, unused features and a class. There are two
things that worth noting, besides simple dead code removal:

1. ControlNode class is removed. We had it as the base class of Group
class, but it provides no functionality particularly meaningful. We now
have shallower class hierarchy that is easier to understand.

2. InputGraph provides a feature to replace a node with its internal data.
It is being used to "expand" some type of node, such as a Linker Script
node, with its actual files. We used to have two options when replacing
it -- ExpandOnly or ExpandAndReplace. ExpandOnly was to expand it but not
remove the node from the tree. There is no use of that option in the code,
so it was a dead feature.

Differential Revision: http://llvm-reviews.chandlerc.com/D3252

llvm-svn: 205363
2014-04-01 21:55:36 +00:00
Shankar Easwaran 9316c40a1b [core] support .gnu.linkonce sections
.gnu.linkonce sections are similar to section groups.

They were supported before section groups existed and provided a way
to resolve COMDAT sections using a different design.

There are few implementations that use .gnu.linkonce sections
to store simple floating point constants which doesnot require complex section
group support but need a way to store only one copy of the floating point
constant in a binary.

.gnu.linkonce based symbol resolution achieves that.

Review : http://llvm-reviews.chandlerc.com/D3242

llvm-svn: 205280
2014-04-01 03:49:55 +00:00
Shankar Easwaran 50136f1f4e Revert "[core] support .gnu.linkonce sections"
This reverts commit 5d5ca72a7876c3dd3dd1db83dc6a0d74be9e2cd1.

Discuss on a better design to raise error when there is a similar group with Gnu
linkonce sections and COMDAT sections.

llvm-svn: 205224
2014-03-31 17:12:06 +00:00
Shankar Easwaran 79cfed55fc [core] support .gnu.linkonce sections
.gnu.linkonce sections are similar to section groups. They were supported before
section groups existed and provided a way to resolve COMDAT sections using a
different design. There are few implementations that use .gnu.linkonce sections
to store simple floating point constants which doesnot require complex section
group support but need a way to store only one copy of the floating point
constant. .gnu.linkonce based symbol resolution achieves that.

llvm-svn: 205163
2014-03-31 03:16:37 +00:00
Rui Ueyama 3907f2a802 [ELF] Support --defsym option to define an absolute symbol.
This patch is to support --defsym option for ELF file format/GNU-compatible
driver. Currently it takes a symbol name followed by '=' and a number. If such
option is given, the driver sets up an absolute symbol with the specified
address. You can specify multiple --defsym options to define multiple symbols.

GNU LD's --defsym provides many more features. For example, it allows users to
specify another symbol name instead of a number to define a symbol alias, or it
even allows a symbol plus an offset (e.g. --defsym=foo+3) to define symbol-
relative alias. This patch does not support that, but will be supported in
subsequent patches.

Differential Revision: http://llvm-reviews.chandlerc.com/D3208

llvm-svn: 205029
2014-03-28 19:02:06 +00:00
Rui Ueyama a9a5129ec1 [ELF] Add --allow-multiple-definition option.
If --allow-multiple-definition option is given, LLD does not treat duplicate
symbol error as a fatal error. GNU LD supports this option.

Differential Revision: http://llvm-reviews.chandlerc.com/D3211

llvm-svn: 205015
2014-03-28 16:26:38 +00:00
Rui Ueyama 200fce4208 Fix typo.
llvm-svn: 204984
2014-03-28 00:59:43 +00:00
Shankar Easwaran 7ac2a3df64 [core] add SectionGroup support
Review : http://llvm-reviews.chandlerc.com/D3182

llvm-svn: 204830
2014-03-26 16:37:13 +00:00
Rui Ueyama 7ad72ebc5e [PECOFF] Support yet another new type of weak symbol.
COMDAT_SELECT_LARGEST is a COMDAT type that make linker to choose the largest
definition from among all of the definition of a symbol. If the size is the
same, the choice is arbitrary.

Differential Revision: http://llvm-reviews.chandlerc.com/D3011

llvm-svn: 204172
2014-03-18 19:37:50 +00:00
Ahmed Charles 13c70b6d4b Replace OwningPtr with std::unique_ptr.
This results in some simplifications to the code where an OwningPtr had to
be used with the previous api and then ownership moved to a unique_ptr for
the rest of lld.

llvm-svn: 203809
2014-03-13 16:20:38 +00:00
Rui Ueyama 7caea31189 Fix a bug that mergeCases table does not match the merge constants.
MergeCases table should not have an entry for MergeContents because atoms with
MergeContents attribute should never have name. This issue was not caught by a
test because getting a value of 6th element of an array of array actually gets
the first element's value of the next array, and that happened to be a valid
value. Added asserts to catch that error.

llvm-svn: 203322
2014-03-08 00:44:01 +00:00
Rui Ueyama c79dd2f80a [PECOFF] Support a new type of weak symbol.
Summary:
COMDAT_SELECT_SAME_SIZE is a COMDAT type that I presume exist only in COFF.
The semantics of the type is that linker should merge such COMDAT sections if
their sizes are the same. Otherwise it's an error.

Reviewers: Bigcheese, shankarke, kledzik

CC: llvm-commits

Differential Revision: http://llvm-reviews.chandlerc.com/D2996

llvm-svn: 203308
2014-03-07 23:05:10 +00:00
Rui Ueyama 16e543bc02 Add "override" and remove "virtual" where appropriate.
For the record, I used clang-modernize to add "override" and perl to remove
"virtual".

llvm-svn: 203164
2014-03-06 21:14:04 +00:00
Ahmed Charles d779459f21 [C++11] Add #include's for OwningPtr.
Allows removing #include's in LLVM while switching to std::unique_ptr.

llvm-svn: 202679
2014-03-03 07:20:05 +00:00
NAKAMURA Takumi 955d27a4ce [CMake] Use target_link_libraries(INTERFACE|PRIVATE) on CMake-2.8.12 to increase opportunity for parallel build.
target_link_libraries(INTERFACE) doesn't bring inter-target dependencies in add_library,
although final targets have dependencies to whole dependent libraries.
It makes most libraries can be built in parallel.

target_link_libraries(PRIVATE) is used to shaared library.
Each dependent library is linked to the target.so, and its user will not see its grandchildren.
For example,

  - libclang.so has sufficient libclang*.a(s).
  - c-index-test requires just only libclang.so.

FIXME: lld is tweaked minimally. Adding INTERFACE in each library would be better thing.
llvm-svn: 202241
2014-02-26 06:53:16 +00:00
Shankar Easwaran 3d8de47f76 Fix trailing whitespace.
llvm-svn: 200182
2014-01-27 03:09:26 +00:00
Rui Ueyama 8d7a6f2898 Fix odd titlecasing and spacing in an error message.
llvm-svn: 199530
2014-01-18 00:57:40 +00:00
Rui Ueyama 251e68b6ae Use auto for readability. No functionality change.
llvm-svn: 199527
2014-01-18 00:38:44 +00:00
Rui Ueyama d9696cbda8 Using "final" as a variable name is a bit confusing in C++11, so rename it.
llvm-svn: 199526
2014-01-18 00:27:03 +00:00
Rafael Espindola d28918b289 Use getError instead of the error_code operator.
llvm-svn: 198797
2014-01-08 22:00:09 +00:00
Joerg Sonnenberger 03ed4cf0a4 Typo
llvm-svn: 198434
2014-01-03 18:43:32 +00:00
Joey Gouly 0518453e2e The return value of createInternalFiles is unused, so remove it.
llvm-svn: 198266
2013-12-31 19:15:42 +00:00
Rui Ueyama fc5f18ba0d Fix a bug that undefined symbols are not reported if dead-stripping is enabled.
There was a bug that the linker does not report an error if symbols specified
by -u (or /include on Windows) are not resolved. This patch fixes it by adding
such symbols to the dead strip root.

llvm-svn: 198041
2013-12-26 08:37:16 +00:00
Rui Ueyama 841016aeba Remove unnecessary call of DenseMap::clear.
It should be always true that _liveAtoms is empty, so we don't have to clear it.
Add an assert() instead.

llvm-svn: 198040
2013-12-26 08:11:06 +00:00
Rui Ueyama e5531fae6c Micro-optimize Resolver::markLive().
This patch eliminates one std::set lookup per a function call.

llvm-svn: 198037
2013-12-26 07:13:28 +00:00
Rui Ueyama c481b5b66d Simplify. No functionality change.
llvm-svn: 198036
2013-12-26 07:02:33 +00:00
Rui Ueyama 0cbd7e0d03 Do not make an unnecessary copy of DenseMap.
llvm-svn: 198035
2013-12-26 06:57:37 +00:00
Rui Ueyama 61b851ab06 Remove duplicate methods.
llvm-svn: 198034
2013-12-26 06:35:35 +00:00
Joey Gouly 6eb02ef568 Make SimpleFileNode inherit from FileNode.
This removes a lot of duplicated code.

llvm-svn: 197751
2013-12-19 23:39:02 +00:00
Nick Kledzik e555277780 [lld] Introduce registry and Reference kind tuple
The main changes are in:
  include/lld/Core/Reference.h
  include/lld/ReaderWriter/Reader.h
Everything else is details to support the main change.

1) Registration based Readers
Previously, lld had a tangled interdependency with all the Readers.  It would
have been impossible to make a streamlined linker (say for a JIT) which
just supported one file format and one architecture (no yaml, no archives, etc).
The old model also required a LinkingContext to read an object file, which
would have made .o inspection tools awkward.

The new model is that there is a global Registry object. You programmatically 
register the Readers you want with the registry object. Whenever you need to 
read/parse a file, you ask the registry to do it, and the registry tries each 
registered reader.

For ease of use with the existing lld code base, there is one Registry
object inside the LinkingContext object. 


2) Changing kind value to be a tuple
Beside Readers, the registry also keeps track of the mapping for Reference
Kind values to and from strings.  Along with that, this patch also fixes
an ambiguity with the previous Reference::Kind values.  The problem was that
we wanted to reuse existing relocation type values as Reference::Kind values.
But then how can the YAML write know how to convert a value to a string? The
fix is to change the 32-bit Reference::Kind into a tuple with an 8-bit namespace
(e.g. ELF, COFFF, etc), an 8-bit architecture (e.g. x86_64, PowerPC, etc), and
a 16-bit value.  This tuple system allows conversion to and from strings with 
no ambiguities.

llvm-svn: 197727
2013-12-19 21:58:00 +00:00
Rui Ueyama 6a2e745351 [PECOFF] Refactor IdataPass.
This patch is to basically move the functionality to construct Data Directory
from IdataPass to WriterPECOFF.

Data Directory is a part of the PE/COFF header and contains the addresses of
the import tables.

We used to represent the link from Data Directory to the import tables as
relocation references. The idea behind it is that, because relocation
references are processed by the Writer, we wouldn't have to do anything special
to fill the addresses of the import tables. I thought that the addresses would
be set "automatically".

But it turned out that that design made the pass and the writer rather
complicated. In order to make relocation references between Data Directory to
the import tables, these data structures needed to be represented as Atom.
However, because Data Directory is not a section content but a part of the
PE/COFF header, it did not fit well as an Atom. So we ended up having
complicated code both in IdataPass and the writer.

This patch simplifies it.

One side effect of this patch is that we now have ".idata.a", ".idata.d" and
"idata.t" sections for the import address table, the import directory table,
and the import lookup table. The writer looks for the sections by name to find
the start addresses of the sections. We probably should have a better way to
find a specific atom from the core linking result, but currently using the
section name seems to be the easiest way to do that. The Windows loader do not
care about the import table's section layout.

llvm-svn: 197016
2013-12-11 08:23:37 +00:00
Rui Ueyama c53b3b0809 Style fixes. No functionality change.
llvm-svn: 196883
2013-12-10 06:19:09 +00:00
Rui Ueyama 2f47acfd6a Make anonymous namespace as small as possible.
Use of static is recommended by the style guide.

llvm-svn: 196877
2013-12-10 05:15:38 +00:00
Rui Ueyama 8b08c3796b Early return.
llvm-svn: 195663
2013-11-25 17:09:29 +00:00
Rui Ueyama 17e899c967 Use range-based for loop.
llvm-svn: 195662
2013-11-25 17:09:27 +00:00
Rui Ueyama 5a3804f9c8 Indentation.
llvm-svn: 195661
2013-11-25 17:09:25 +00:00
Shankar Easwaran d87a021c79 [InputGraph][Gnu] Add LinkerScript support.
This adds LinkerScript support by creating a type Script which is of type
FileNode in the InputGraph. Once the LinkerScript Parser converts the
LinkerScript into a sequence of command, the commands are handled by the
equivalent LinkerScript node for the current Flavor/Target. For ELF, a
ELFGNULdScript gets created which converts the commands to ELF nodes and ELF
control nodes(ELFGroup for handling Group nodes).

Since the Inputfile type has to be determined in the Driver, the Driver needs
to determine the complete path of the file that needs to be processed by the
Linker. Due to this, few tests have been removed since the Driver uses paths
that doesnot exist.

llvm-svn: 195583
2013-11-24 23:12:36 +00:00
Shankar Easwaran 67e98f5197 [InputGraph] Add capability to process Hidden nodes.
Hidden nodes could be a result of expansion, where a flavor might decide to keep
the node that we want to expand but discard it from being processed by the
resolver.

Verifies with unittests.

llvm-svn: 195516
2013-11-22 23:19:53 +00:00
Shankar Easwaran 3ac09bcb8f [InputGraph] Expand InputGraph nodes.
Flavors may like to expand InputGraph nodes, when a filenode after parsing
results in more elements. One such example is while parsing GNU linker scripts.
The linker scripts after parsing would result in a lot of filenodes and probably
controlnodes too.

Adds unittests to verify functionality.

llvm-svn: 195515
2013-11-22 23:08:24 +00:00
Rui Ueyama fd133b300c Fix indentation and whitespace.
llvm-svn: 195389
2013-11-21 23:41:53 +00:00
Shankar Easwaran 178324d903 [LinkingContext] Limit shared library undefined atoms to be added.
This adds functionality to limit shared library undefined atoms to be added
only once by the Resolver.

Dynamic libraries may be processed more than once if they exist within a
Group.

Also adds a test to verify the change.

llvm-svn: 195307
2013-11-21 03:50:59 +00:00
Rui Ueyama 70f11d7589 Fix Weak External symbol handling.
The fallback atom was used only when it's searching for a symbol in a library;
if an undefined symbol was not found in a library, the LLD looked for its
fallback symbol in the library.

Although it worked in most cases, because symbols with fallbacks usually occur
only in OLDNAMES.LIB (a standard library), that behavior was incompatible with
link.exe. This patch fixes the issue so that the semantics is the same as
MSVC's link.exe

The new (and correct, I believe) behavior is this:

 - If there's no definition for an undefined atom, replace the undefined atom
   with its fallback and then proceed (e.g. look in the next file or stop
   linking as usual.)

Weak External symbols are underspecified in the Microsoft PE/COFF spec. However,
as long as I observed the behavior of link.exe, this seems to be what we want
for compatibility.

Differential Revision: http://llvm-reviews.chandlerc.com/D2162

llvm-svn: 195269
2013-11-20 20:51:55 +00:00
Rui Ueyama d9b26dc84b Remove extraneous curly braces and blank lines.
llvm-svn: 195107
2013-11-19 06:18:39 +00:00
Rui Ueyama 69c71cc827 Simplify. No functionality change.
llvm-svn: 195051
2013-11-18 22:42:03 +00:00
Rui Ueyama 3429d01f1a Refactor copy-paste-and-modifed code using callback.
llvm-svn: 194784
2013-11-15 04:58:54 +00:00
Rui Ueyama b4dca7f065 Select new undefined atom rather than old one if other conditions are the same.
We can add multiple undefined atoms having the same name to the symbol table.
If such atoms are added, the symbol table compares their canBeNull attributes,
and select one having a stronger constraint. If their canBeNulls are the same,
the choice is arbitrary. Currently it choose the existing one.

This patch changes the preference, so that the symbol table choose the new one
if the new atom has a greater canBeNull or a fallback atom. This shouldn't
change the behavior except the case described below.

A new undefined atom may have a new fallback atom attribute. By choosing the new
atom, we can update the fallback atom during Core Linking. PE/COFF actually need
that. For example, _lseek is an alias for __lseek on Windows. One of an object
file in OLDNAMES.LIB has an undefined atom for _lseek with the fallback to
__lseek. When the linker tries to resolve _read, it supposed to read the file
from OLDNAMES.LIB and use the new fallback from the file. Currently LLD cannot
handle such case because duplicate undefined atoms with the same attributes are
ignored.

Differential Revision: http://llvm-reviews.chandlerc.com/D2161

llvm-svn: 194777
2013-11-15 03:12:24 +00:00
Rui Ueyama 9310e01ea9 Terminate if there are un-mergeable duplicate atoms.
llvm-svn: 194671
2013-11-14 06:39:31 +00:00
Michael J. Spencer bb0919968b Move InputGraph from Driver to Core. LinkingContext depends on it.
llvm-svn: 194641
2013-11-14 00:51:33 +00:00
Rui Ueyama 0acd8243e3 Remove default label from fully covered switch.
llvm-svn: 194624
2013-11-13 23:33:49 +00:00
Rui Ueyama bcccb5db2e Show error message if two atoms are not mergeable.
llvm-svn: 194620
2013-11-13 23:23:38 +00:00
Rui Ueyama f347e7533f Fix indentation, use early return.
llvm-svn: 194619
2013-11-13 23:22:00 +00:00
Rui Ueyama e20474d38c Revert "Use empty() instead of size() == 0."
This reverts commit r194551 because it broke the buildbot.

llvm-svn: 194552
2013-11-13 03:30:29 +00:00
Rui Ueyama 2235bff2b9 Use empty() instead of size() == 0.
llvm-svn: 194551
2013-11-13 03:09:20 +00:00
Nick Kledzik 471c00cfcb Hopefully fix latent bug where lldCore dependency on lldYAML was not in CMake files
llvm-svn: 194177
2013-11-06 22:56:42 +00:00
Rui Ueyama c1800beb55 Remove unnecessary namespace qualifier.
llvm-svn: 194037
2013-11-05 01:37:40 +00:00
Rafael Espindola 75c2ae9664 Replace ErrorOr<void> with error_code.
It was never transporting any value in addition to the error_code.

llvm-svn: 194028
2013-11-05 00:09:36 +00:00
Shankar Easwaran 2bc24928d3 [PassManager] add ReaderWriter{Native,YAML} to the Driver.
Enable this for the following flavors

a) core
b) gnu
c) darwin

Its disabled for the flavor PECOFF. Convenient markers are added with FIXME
comments in the Driver that would be removed and code removed from each flavor.

llvm-svn: 193585
2013-10-29 05:12:14 +00:00
Rui Ueyama 671c8013ed Revert "r193300 - [PassManager] add ReaderWriter{Native, YAML} to the Driver"
The patch have completely broken COFF port and disabled many tests.
This also reverts r193302 (comment fix).

llvm-svn: 193362
2013-10-24 18:22:16 +00:00
Shankar Easwaran 89c2d8faca [PassManager] add ReaderWriter{Native,YAML} to the Driver.
Disable tests to be run with REQUIRES: disable. Note disable is not added to the
config by the test runner Mkaefiles, so essentially disables the test.

Code changes would be required to fix these tests :-

test/darwin/hello-world.objtxt
test/elf/check.test
test/elf/phdr.test
test/elf/ppc.test
test/elf/undef-from-main-dso.test
test/elf/X86_64/note-sections-ro_plus_rw.test
test/pecoff/alignment.test
test/pecoff/base-reloc.test
test/pecoff/bss-section.test
test/pecoff/drectve.test
test/pecoff/dynamic.test
test/pecoff/dynamicbase.test
test/pecoff/entry.test
test/pecoff/hello.test
test/pecoff/imagebase.test
test/pecoff/importlib.test
test/pecoff/lib.test
test/pecoff/multi.test
test/pecoff/reloc.test
test/pecoff/weak-external.test

llvm-svn: 193300
2013-10-24 03:30:03 +00:00
Rui Ueyama f3630fe4be Make undefines check into an assertion.
Dead-strip root symbols can be undefined atoms, but should not really be
nonexistent, because dead-strip root symbols should be added to initial
undefined atoms at startup. Whenever you look up its name in the symbol
table, some type of atom will always exist.

llvm-svn: 192831
2013-10-16 19:21:50 +00:00
Rui Ueyama 2847831771 Propagate deadStripOptimize()'s failure to the caller.
We want to make the program to exit with non-zero exit code if there's an error
during dead stripping.

llvm-svn: 192771
2013-10-16 05:03:39 +00:00
Rui Ueyama 2ad117db97 Propagate Resolver::resolveUndefines()'s failure to caller.
llvm-svn: 192424
2013-10-11 06:26:16 +00:00
Rui Ueyama ee366042e9 Return true from Resolver::resolve() on success.
llvm-svn: 192422
2013-10-11 06:16:33 +00:00
Rui Ueyama a4770543f1 Remove an unused variable added in r192415.
llvm-svn: 192416
2013-10-11 03:51:19 +00:00
Rui Ueyama d542b9e53a Use switch instead of if, and handle all enum values.
This patch also handles errors other than no_more_files error. They were silently
ignored.

llvm-svn: 192415
2013-10-11 03:48:06 +00:00
Shankar Easwaran 49408ece47 [Layout] Dont set ordinals to Files by default.
This change removes code in various places which was setting the File Ordinals.
This is because the file ordinals are assigned by the way files are resolved.

There was no other way than making the getNextFileAndOrdinal be set const and
change the _nextOrdinal to mutable.

There are so many places in code, that you would need to cleanup to make
LinkingContext non-const!

llvm-svn: 192280
2013-10-09 07:32:47 +00:00
Shankar Easwaran 1d3c48f1fc [Layout] Assign ordinals in Resolution order.
llvm-svn: 192277
2013-10-09 05:23:23 +00:00
Shankar Easwaran 03f7763d21 [inputGraph] Associate Resolve state with appropriate nodes
This associates resolveState to FileNodes. The control node derive
their resolution state from the inputElements that are contained in
it.

This makes --start-group/--end-group to work with ELF linking.

llvm-svn: 192269
2013-10-09 03:40:29 +00:00
Rui Ueyama c6015f6476 Update error classes from all lowercase to camel case.
llvm-svn: 192261
2013-10-09 00:57:22 +00:00
Rui Ueyama 1c1b443997 Move a local variable into while-loop scope.
llvm-svn: 192260
2013-10-09 00:42:57 +00:00
Shankar Easwaran 7f1146c494 [LinkingContext] make LinkingContext non-const
llvm-svn: 192183
2013-10-08 15:43:48 +00:00
Rui Ueyama 8b8e5216af Make bitmask an unsigned type.
llvm-svn: 192163
2013-10-08 03:59:45 +00:00
Rui Ueyama 094546836b Add comments. Early return.
llvm-svn: 192149
2013-10-08 00:43:53 +00:00