Commit Graph

1209 Commits

Author SHA1 Message Date
Rui Ueyama fa561713f3 [PECOFF] Parse /export optional arguments.
/EXPORT command line option can take an ordinal, NONAME flag, and DATA flag.
This patch is to parse these optional arguments.

llvm-svn: 197217
2013-12-13 08:42:52 +00:00
Rui Ueyama c4123a5782 Run clang-format on the new files.
I should have run it before submitting but forgot to do that. Doing it now...

llvm-svn: 197214
2013-12-13 07:37:52 +00:00
Rui Ueyama 3a136547d7 [PECOFF] Align .edata fields on natural boundaries.
The only data in .edata whose length varies is the string. This patch moves
all the strings to the end of the section, so that 16-bit or 32-bit integers
are aligned on correct boundaries.

llvm-svn: 197213
2013-12-13 07:34:40 +00:00
Rui Ueyama c91c24e33d [PECOFF] Create .edata section for the DLL export table.
This is the first patch to emit data for the DLL export table. The DLL export
table is the data used by the Windows loader to find the address of exported
function from DLL. With this patch, LLD is able to emit a valid DLL export
table which the Windows loader can interpret and load.

The data structure of the DLL export table is described in the Microsoft
PE/COFF Specification, section 5.3.

DLL support is not complete yet; the linker needs to emit an import library
for a DLL, otherwise the linker cannot link against the DLL. We also do not
support export-only-by-ordinal yet.

llvm-svn: 197212
2013-12-13 06:58:27 +00:00
Rui Ueyama 091071ff0b [PECOFF] Rename lld::coff -> lld::pecoff.
We had lld::coff and lld::pecoff namespaces for no reason. Unify them.

llvm-svn: 197201
2013-12-13 02:58:27 +00:00
Rui Ueyama d68304eeee [PECOFF] Move a utility function used in a pass to Pass.cpp.
The file currently has only one function. Function that is useful both for
IdataPass and EdataPass will be added to that file.

llvm-svn: 197140
2013-12-12 10:01:14 +00:00
Rui Ueyama a92e2311bb [PECOFF] Replace DLLNameAtom with COFFStringAtom.
DLLNameAtom is an atom whose content is a string. IdataAtom is not going to
be the only place we need such atom, so I want to generalize it.

llvm-svn: 197137
2013-12-12 08:53:46 +00:00
Rui Ueyama 5a10da129f [PECOFF] Rename IdataPassFile and move it to Atoms.h.
I'm planning to create a new pass for the DLL export table, and I want to use
the class both from IdataPass and the new pass, EdataPass. So move the class to
a common place.

llvm-svn: 197132
2013-12-12 06:58:00 +00:00
Rui Ueyama d3199fdd2e [PECOFF] Parse /dll command line option.
llvm-svn: 197123
2013-12-12 03:21:45 +00:00
Rui Ueyama 4cf5a16117 [PECOFF] Add /dllexport option.
/DLLEXPORT is a command line option to export a symbol. __declspec(dllexport)
uses that to make the linker to export DLLExport'ed functions, by adding the
option to .drectve section.

This patch implements the parser of the command line option.

llvm-svn: 197122
2013-12-12 03:11:26 +00:00
Rui Ueyama 9e7b3cb024 Add .clang-format file to ensure C++11 LLVM coding style.
llvm-svn: 197085
2013-12-11 22:39:33 +00:00
Rui Ueyama bb08e62dd6 Run clang-format for PECOFF reader/writer code. No other changes.
llvm-svn: 197039
2013-12-11 14:10:25 +00:00
Rui Ueyama 93961d8e2a [PECOFF] Writer: Refactor the chunk class hierarchy.
Before this patch, we had the following class hierarchy.

  Chunk -> AtomChunk -> SectionChunk -> GenericSectionChunk
                                     -> BaseRelocChunk
        -> HeaderChunk

Chunk represented the generic concept of contiguous range in an output
file. AtomChunk represented a chunk consists of atoms.

That class hierarchy had many issues: 1) BaseRelocChunk does not really
consist of atoms, so inheriting from AtomChunk was plainly wrong, and 2)
the hierarchy is unecessarily too deep.

This patch correct them. The new hierachy is shown below.

  Chunk -> SectionChunk -> AtomChunk
                        -> BaseRelocChunk
        -> HeaderChunk

In the new hierarchy, AtomChunk represents a chunk consists of atoms. Other
types of sections (currently only BaseRelocChunk) should inherit directly
from SectionChunk.

llvm-svn: 197038
2013-12-11 14:00:10 +00:00
Rui Ueyama 5dd609206d [PECOFF] Add "const" qualifiers to BaseRelocChunk methods.
Also removed unused field.

llvm-svn: 197027
2013-12-11 10:57:36 +00:00
Rui Ueyama 338d70c0bb [PECOFF] Writer: Remove BaseRelocAtom.
No functionality change.

llvm-svn: 197025
2013-12-11 10:44:04 +00:00
Rui Ueyama 7e4660508d [PECOFF] Writer: Remove rawSize().
Because sections no longer have trailing NULL bytes, size() and rawSize() now
return the same value.

llvm-svn: 197020
2013-12-11 09:00:08 +00:00
Rui Ueyama 148049b0e8 [PECOFF] Remove enum for Data Directory atom which no longer exist.
llvm-svn: 197019
2013-12-11 08:53:24 +00:00
Rui Ueyama 1e26c74878 [PECOFF] Remove code which is no longer needed because of r197016.
llvm-svn: 197018
2013-12-11 08:40:40 +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 64a406b20b Simplify code a bit. No functionality change.
llvm-svn: 197009
2013-12-11 04:58:34 +00:00
Rui Ueyama f946424bd8 [PECOFF] Writer: Move SectionChunk's ctor inline.
llvm-svn: 197008
2013-12-11 04:36:19 +00:00
Rui Ueyama a63760592b Use "static" instead of anonymous namespace.
llvm-svn: 197007
2013-12-11 04:30:15 +00:00
Rui Ueyama 65827a9f77 [PECOFF] Make a member functions non-virtual.
llvm-svn: 197006
2013-12-11 04:30:06 +00:00
Rui Ueyama 0d4d40cfee [PECOFF] Writer: Remove NULL padding at the end of each section.
If section size is not multiple of 512, the writer added NULL bytes at the end
of it to make it so. That is not required by the PE/COFF spec, and the MSVC's
linker does not do that too. So we don't need to do that, too.

llvm-svn: 197002
2013-12-11 04:06:26 +00:00
Rui Ueyama 5c65a0efd1 s/NULL/nullptr/
llvm-svn: 196994
2013-12-11 01:31:54 +00:00
Rui Ueyama 2b7bb6c1f1 [PECOFF] WriterPECOFF: Rename getCharacteristics -> computeCharacteristics.
The base class has a member function with the same name. We should avoid it
being shadowed.

llvm-svn: 196992
2013-12-11 01:17:11 +00:00
Rui Ueyama 4b6b266823 [PECOFF] WriterPECOFF::ExecutableWriter: Small cleanup.
llvm-svn: 196991
2013-12-11 01:07:52 +00:00
Rui Ueyama 0bf1381902 Add explicit keyword.
llvm-svn: 196898
2013-12-10 09:12:07 +00:00
Rui Ueyama 3619f00ad5 [PECOFF] Make more member functions non-virtual.
llvm-svn: 196897
2013-12-10 09:02:00 +00:00
Rui Ueyama 9c922dd6ae [PECOFF] Optimize the writer a bit by removing a loop.
llvm-svn: 196896
2013-12-10 08:59:12 +00:00
Rui Ueyama d9d4be6993 [PECOFF] Refactor COFF section header creation.
Code to create COFF section header was scattered across many member functions
of SectionChunk. Consolidate it to a member function of SectionHeaderTableChunk.

llvm-svn: 196895
2013-12-10 08:39:06 +00:00
Rui Ueyama 27964e663b Add more const qualifiers.
llvm-svn: 196891
2013-12-10 07:15:57 +00:00
Rui Ueyama 17e663154a Remove data members used for relocations
... because they are used only in the function for relocations.

llvm-svn: 196890
2013-12-10 06:54:13 +00:00
Rui Ueyama 906d93432e Skip the body of a loop as early as possible.
llvm-svn: 196884
2013-12-10 06:32:21 +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 5ae0e5bffc Add const qualifiers.
llvm-svn: 196867
2013-12-10 03:57:59 +00:00
Rui Ueyama e46a06361c [PECOFF] Make some member functions non-virtual.
These member functions are not overriden and not intended to be, so adding
virtual does not make sense.

llvm-svn: 196866
2013-12-10 03:17:51 +00:00
Rui Ueyama c12f2a1121 Revert "Remove makeArrayRef() calls."
This reverts commit r196475 because it made the build to fail with
GCC 4.7/4.8/4.9. Reported by Mikael Lyngvig.

llvm-svn: 196853
2013-12-10 00:42:52 +00:00
Joey Gouly 34ec7f7411 Fix a copy/paste error.
llvm-svn: 196770
2013-12-09 10:23:27 +00:00
Rui Ueyama 681cabfc75 [PECOFF] Simplify PE/COFF header writer.
llvm-svn: 196767
2013-12-09 09:02:10 +00:00
Rui Ueyama d4339076c6 [PECOFF] Simplify WriterPECOFF.
llvm-svn: 196762
2013-12-09 08:47:20 +00:00
Rui Ueyama dc3e6d2c1b Fix broken test.
llvm-svn: 196756
2013-12-09 05:18:56 +00:00
Rui Ueyama 61580376b6 [PECOFF] Implement /alternatename weak symbols.
llvm-svn: 196754
2013-12-09 05:02:57 +00:00
Rui Ueyama 34d6e9b371 [PECOFF] Add /alternatename option parser.
/ALTERNATENAME is a rarely-used, undocumented command line option that is
needed to link LLD for release build. It seems that the option is for defining
an weak alias; /alternatename:foo=bar defines weak symbol "foo" for "bar".
If "foo" is defined in an input file, it'll be linked normally and the command
line option will have no effect. If it's not defined, "foo" will be handled
as an alias for "bar".

This patch implements the parser for the option. The actual weak alias handling
will be implemented in a separate patch.

llvm-svn: 196743
2013-12-09 01:47:32 +00:00
Rui Ueyama a930d12fe3 Move scattered debug functions into one #ifndef-guarded place.
llvm-svn: 196741
2013-12-09 00:37:19 +00:00
Rui Ueyama 2994f6f772 Fix -Wunused-function to unbreak buildbot.
llvm-svn: 196716
2013-12-08 03:37:58 +00:00
Rui Ueyama 5af4622f30 Move static member functions out of a class.
Because compare() and its heper functions no longer have to be members of
LayoutPass class, we can remove it from the class. No functionality change.

llvm-svn: 196715
2013-12-08 03:24:09 +00:00
Rui Ueyama 37c43e9f21 Optimize the layout pass.
The comparator used in the layout pass has many calls of map::find(). Because
std::sort runs the comparator N*log2(N) times, the maps are looked up with the
same key again and again. The map lookup is not a very fast operation. It made
the pass slow.

This patch eliminates the duplicate map lookups using decorate-sort-undecorate
idiom. The pass used to take 1.1 seconds when linking LLD with LLD on Windows,
but it now takes only 0.3 seconds. Overall performance gain in that case is from
6.1 seconds to 5.2 seconds.

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

llvm-svn: 196714
2013-12-08 03:12:08 +00:00
Rui Ueyama 32c3f17d36 Re-submit r195852 with GroupedSectionsPass change.
GroupedSectionsPass was a complicated pass. That pass's job was to reorder
atoms by section name, so that the atoms with the same section prefix will be
emitted consecutively to the executable. The pass added layout edges to atoms,
and let the layout pass to actually reorder them.

This patch simplifies the design by making GroupedSectionPass to directly
reorder atoms, rather than adding layout edges. This resembles ELF's
ArrayOrderPass.

This patch improves the performance of LLD; it used to take 7.1 seconds to
link LLD with LLD on my Macbook Pro, but it now takes 6.1 seconds.

llvm-svn: 196628
2013-12-07 00:27:17 +00:00