Commit Graph

82 Commits

Author SHA1 Message Date
Rafael Espindola aae5541455 Don't iterate over all sections in the ELFFile constructor.
With this we finally have an ELFFile that is O(1) to construct. This is helpful
for programs like lld which have to do their own section walk.

llvm-svn: 244510
2015-08-10 21:29:35 +00:00
Rafael Espindola 94515abfd7 elf2yaml: Use existing section walk to find the symbol table. NFC.
llvm-svn: 244447
2015-08-10 14:27:50 +00:00
Rafael Espindola 4001998455 Remove the symbol iteration functions that don't take a symbol table.
Another step in making ELFFile's constructor not iterate over all sections.

llvm-svn: 244351
2015-08-07 20:07:27 +00:00
Rafael Espindola ed6023a964 Use helper function. NFC.
llvm-svn: 243012
2015-07-23 12:51:44 +00:00
Rafael Espindola 651ed6c7d8 Add a version of getSymbol with an explicit symbol table. Use it. NFC.
llvm-svn: 243011
2015-07-23 12:49:40 +00:00
Rafael Espindola bd05101e67 Remove getStaticSymbolName.
Every user now keeps track of the correct string table to use.

llvm-svn: 242818
2015-07-21 18:04:29 +00:00
Rafael Espindola 34d17fb54d Remove always false parameter.
llvm-svn: 242802
2015-07-21 16:42:01 +00:00
Rafael Espindola fe35b9d472 Use range loop. NFC.
llvm-svn: 242801
2015-07-21 16:40:14 +00:00
Simon Atanasyan 5db0276925 [ELFYAML] Fix handling SHT_NOBITS sections by obj2yaml/yaml2obj tools
SHT_NOBITS sections do not have content in an object file. Now the yaml2obj
tool does not accept `Content` field for such sections, and the obj2yaml
tool does not attempt to read the section content from a file.

Restore r241350 and r241352.

llvm-svn: 241377
2015-07-03 23:00:54 +00:00
Rafael Espindola e9da9aa4f3 This reverts commit r241350 and r241352.
r241350 broke lld tests.
r241352 depends on r241350.

Original messages:
"[ELFYAML] Fix handling SHT_NOBITS sections by obj2yaml/yaml2obj tools"
"[ELFYAML] Make the Size field for .bss section optional"

llvm-svn: 241354
2015-07-03 14:54:02 +00:00
Simon Atanasyan b776eaed2e [ELFYAML] Fix handling SHT_NOBITS sections by obj2yaml/yaml2obj tools
SHT_NOBITS sections do not have content in an object file. Now yaml2obj
tool does not accept `Content` field for such sections, and obj2yaml
tool does not attempt to read the section content from a file.

llvm-svn: 241350
2015-07-03 14:07:06 +00:00
Rafael Espindola 5d0c2ffadf Return ErrorOr from SymbolRef::getName.
This function can really fail since the string table offset can be out of
bounds.

Using ErrorOr makes sure the error is checked.

Hopefully a lot of the boilerplate code in tools/* can go away once we have
a diagnostic manager in Object.

llvm-svn: 241297
2015-07-02 20:55:21 +00:00
Rafael Espindola 6def304209 Return ErrorOr from getSection.
This also improves the logic of what is an error:

* getSection(uint_32): only return an error if the index is out of bounds. The
  index 0 corresponds to a perfectly valid entry.
* getSection(Elf_Sym): Returns null for symbols that normally don't have
  sections and error for out of bound indexes.

In many places this just moves the report_fatal_error up the stack, but those
can then be fixed in smaller patches.

llvm-svn: 241156
2015-07-01 12:56:27 +00:00
Rafael Espindola 2b5f841e98 Fix the name of the iterator functions to match the coding standards.
llvm-svn: 241074
2015-06-30 15:33:44 +00:00
Rafael Espindola 44c2871c09 Convert obj->getSymbolName to sym->getName.
I doesn't depend on the object anymore.

llvm-svn: 240996
2015-06-29 21:24:55 +00:00
Rafael Espindola 6a1bfb2f9b Factor out the checking of string tables.
This moves the error checking for string tables to getStringTable which returns
an ErrorOr<StringRef>.

This improves error checking, makes it uniform across all string tables and
makes it possible to check them once instead of once per name.

llvm-svn: 240950
2015-06-29 14:39:25 +00:00
Rafael Espindola 719dc7c436 Remove Elf_Sym_Iter.
It was a fairly broken concept for an ELF only class.

An ELF file can have two symbol tables, but they have exactly the same
format. There is no concept of a dynamic or a static symbol. Storing this
on the iterator also makes us do more work per symbol than necessary. To fetch
a name we would:

* Find if we had a static or a dynamic symbol.
* Look at the corresponding symbol table and find the string table section.
* Look at the string table section to fetch its contents.
* Compute the name as a substring of the string table.

All but the last step can be done per symbol table instead of per symbol. This
is a step in that direction.

llvm-svn: 240939
2015-06-29 12:38:31 +00:00
Rui Ueyama 7d09919534 Remove object_error::success and use std::error_code() instead
make_error_code(object_error) is slow because object::object_category()
uses a ManagedStatic variable. But the real problem is that the function is
called too frequently. This patch uses std::error_code() instead of
object_error::success. In most cases, we return "success", so this patch
reduces number of function calls to that function.

http://reviews.llvm.org/D10333

llvm-svn: 239409
2015-06-09 15:20:42 +00:00
Simon Atanasyan 04d9e653ed [obj2yaml/yaml2obj] Add SHT_MIPS_ABIFLAGS section support
This change adds support for the SHT_MIPS_ABIFLAGS section
reading/writing to the obj2yaml and yaml2obj tools.

llvm-svn: 236738
2015-05-07 15:40:48 +00:00
Shankar Easwaran 6fbbe20176 [obj2yaml/yaml2obj] Add SHT_GROUP support.
This adds section group support to the tools obj2yaml and yaml2obj.

llvm-svn: 230124
2015-02-21 04:28:26 +00:00
David Majnemer f69b0585c1 obj2yaml, yaml2obj: Add support for COFF executables
In support of serializing executables, obj2yaml now records the virtual address
and size of sections.  It also serializes whatever we strictly need from
the PE header, it expects that it can reconstitute everything else via
inference.

yaml2obj can reconstitute a fully linked executable.

In order to get executables correctly serialized/deserialized, other
bugs were fixed as a circumstance.  We now properly respect file and
section alignments.  We also avoid writing out string tables unless they
are strictly necessary.

llvm-svn: 221975
2014-11-14 08:15:42 +00:00
Simon Atanasyan 60e1a79242 [ELF][yaml2obj] Handle additional MIPS specific st_other field flags
The ELF symbol `st_other` field might contain additional flags besides
visibility ones. This patch implements support for some MIPS specific
flags.

llvm-svn: 221491
2014-11-06 22:46:24 +00:00
Simon Atanasyan 68a79a790c Add accessor to get 'visibility' part of st_other field
This new `getVisibility()` function will also be used in the LLD code.

llvm-svn: 221392
2014-11-05 20:47:35 +00:00
David Majnemer c7d7c6fb3a Object, COFF: Cleanup symbol type code, improve binutils compatibility
Do a better job classifying symbols.  This increases the consistency
between the COFF handling code and the ELF side of things.

llvm-svn: 220952
2014-10-31 05:07:00 +00:00
David Majnemer efbe94890c obj2yaml, COFF: Handle long section names
Long section names are represented as a slash followed by a numeric
ASCII string.  This number is an offset into a string table.

Print the appropriate entry in the string table instead of the less
enlightening /4.

N.B.  yaml2obj already does the right thing, this test exercises both
sides of the (de-)serialization.

llvm-svn: 219458
2014-10-10 00:17:57 +00:00
David Majnemer 4d57159c09 MC: Add support for BigObj
Teach WinCOFFObjectWriter how to write -mbig-obj style object files;
these object files allow for more sections inside an object file.

Our support for BigObj is notably different from binutils and cl: we
implicitly upgrade object files to BigObj instead of asking the user to
compile the same file *again* but with another flag.  This matches up
with how LLVM treats ELF variants.

This was tested by forcing LLVM to always emit BigObj files and running
the entire test suite.  A specific test has also been added.

I've lowered the maximum number of sections in a normal COFF file,
VS "14" CTP 3 supports no more than 65279 sections.  This is important
otherwise we might not switch to BigObj quickly enough, leaving us with
a COFF file that we couldn't link.

yaml2obj support is all that remains to implement.

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

llvm-svn: 217812
2014-09-15 19:42:42 +00:00
Ismail Pazarbasi 1379b96d4c Added missing LLVM_NOEXCEPT to the definition of _obj2yaml_error_category::name
LLVM_NOEXCEPT was added in r210591.

llvm-svn: 217603
2014-09-11 17:19:54 +00:00
David Majnemer 44f51e5113 Object: Add support for bigobj
This adds support for reading the "bigobj" variant of COFF produced by
cl's /bigobj and mingw's -mbig-obj.

The most significant difference that bigobj brings is more than 2**16
sections to COFF.

bigobj brings a few interesting differences with it:
- It doesn't have a Characteristics field in the file header.
- It doesn't have a SizeOfOptionalHeader field in the file header (it's
  only used in executable files).
- Auxiliary symbol records have the same width as a symbol table entry.
  Since symbol table entries are bigger, so are auxiliary symbol
  records.

Write support will come soon.

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

llvm-svn: 217496
2014-09-10 12:51:52 +00:00
Rafael Espindola 48af1c2a1a Don't own the buffer in object::Binary.
Owning the buffer is somewhat inflexible. Some Binaries have sub Binaries
(like Archive) and we had to create dummy buffers just to handle that. It is
also a bad fit for IRObjectFile where the Module wants to own the buffer too.

Keeping this ownership would make supporting IR inside native objects
particularly painful.

This patch focuses in lib/Object. If something elsewhere used to own an Binary,
now it also owns a MemoryBuffer.

This patch introduces a few new types.

* MemoryBufferRef. This is just a pair of StringRefs for the data and name.
  This is to MemoryBuffer as StringRef is to std::string.
* OwningBinary. A combination of Binary and a MemoryBuffer. This is needed
  for convenience functions that take a filename and return both the
  buffer and the Binary using that buffer.

The C api now uses OwningBinary to avoid any change in semantics. I will start
a new thread to see if we want to change it and how.

llvm-svn: 216002
2014-08-19 18:44:46 +00:00
Benjamin Kramer a7c40ef022 Canonicalize header guards into a common format.
Add header guards to files that were missing guards. Remove #endif comments
as they don't seem common in LLVM (we can easily add them back if we decide
they're useful)

Changes made by clang-tidy with minor tweaks.

llvm-svn: 215558
2014-08-13 16:26:38 +00:00
Rafael Espindola 3f6481d0d3 Remove some calls to std::move.
Instead of moving out the data in a ErrorOr<std::unique_ptr<Foo>>, get
a reference to it.

Thanks to David Blaikie for the suggestion.

llvm-svn: 214516
2014-08-01 14:31:55 +00:00
Rafael Espindola 437b0d5887 Use std::unique_ptr to make the ownership explicit.
llvm-svn: 214377
2014-07-31 03:12:45 +00:00
Simon Atanasyan b5adb21240 Install the `obj2yaml` and `yaml2obj` utilities together with other LLVM tools.
llvm-svn: 214191
2014-07-29 18:28:16 +00:00
Rafael Espindola 97de474a36 Invert the MC -> Object dependency.
Now that we have a lib/MC/MCAnalysis, the dependency was there just because
of two helper classes. Move the two over to MC.

This will allow IRObjectFile to parse inline assembly.

llvm-svn: 212248
2014-07-03 02:01:39 +00:00
Rafael Espindola 54f1997979 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 object_error::parse_failed to
std::errc::invalid_argument.

llvm-svn: 210912
2014-06-13 15:36:17 +00:00
Rafael Espindola 4453e42945 Remove 'using std::error_code' from tools.
llvm-svn: 210876
2014-06-13 03:07:50 +00:00
Rafael Espindola bff5d0d16a Remove all uses of 'using std::error_code' from headers.
llvm-svn: 210866
2014-06-13 01:25:41 +00:00
Rafael Espindola 3acea39853 Don't use 'using std::error_code' in include/llvm.
This should make sure that most new uses use the std prefix.

llvm-svn: 210835
2014-06-12 21:46:39 +00:00
Rafael Espindola a6e9c3e43a Remove system_error.h.
This is a minimal change to remove the header. I will remove the occurrences
of "using std::error_code" in a followup patch.

llvm-svn: 210803
2014-06-12 17:38:55 +00:00
Rafael Espindola 0a5f9cf50d Replace llvm::error_code with std::error_code.
llvm-svn: 210783
2014-06-12 14:11:22 +00:00
Rafael Espindola 25188c95de Don't import error_category into the llvm namespace.
llvm-svn: 210733
2014-06-12 01:45:43 +00:00
Rafael Espindola acc5d7c911 Don't import error_condition into the llvm namespace.
llvm-svn: 210731
2014-06-12 01:29:42 +00:00
Rafael Espindola 5c4f829424 Use std::error_code instead of llvm::error_code.
The idea of this patch is to turn llvm/Support/system_error.h into a
transitional header that just brings in the erorr_code api to the llvm
namespace. I will remove it shortly afterwards.

The cases where the general idea needed some tweaking:

* std::errc is a namespace in msvc, so we cannot use "using std::errc". I could
add an #ifdef, but there were not that many uses, so I just added std:: to
them in this patch.

* Template specialization had to be moved to the std namespace in this
patch set already.

* The msvc implementation of default_error_condition doesn't seem to
provide the same transformations as we need. Not too surprising since
the standard doesn't actually say what "equivalent" means. I fixed the
problem by keeping our old mapping and using it at error_code
construction time.

Despite these shortcomings I think this is still a good thing. Some reasons:

* The different implementations of system_error might improve over time.
* It removes 925 lines of code from llvm already.
* It removes 6313 bytes from the text segment of the clang binary when
it is built with gcc and 2816 bytes when building with clang and
libstdc++.

llvm-svn: 210687
2014-06-11 19:05:50 +00:00
Rafael Espindola 674910d331 Use an enum class.
llvm-svn: 210620
2014-06-11 01:09:09 +00:00
Rafael Espindola f5d07fa586 Mark a few functions noexcept.
This reduces the difference between std::error_code and llvm::error_code.

llvm-svn: 210591
2014-06-10 21:26:47 +00:00
Simon Atanasyan 65e6467e69 [yaml2obj][obj2yaml] Support ELF symbol's visibility flags (default/hidden/protected).
llvm-svn: 210316
2014-06-06 07:41:57 +00:00
Rafael Espindola a3f2e3f01e There is no std::errc::success, remove the llvm one.
llvm-svn: 209960
2014-05-31 03:21:04 +00:00
Simon Atanasyan 8745993ec4 [elf2yaml][ELF] Move Info field to the RelocationSection structure. This
field represents ELF section header sh_info field and does not have any
sense for regular sections. Its interpretation depends on section type.

llvm-svn: 209801
2014-05-29 11:05:31 +00:00
Simon Atanasyan b83f380ae4 [yaml2obj][ELF] Add an optional `Size` field to the YAML section declaration.
Now the only method to configure ELF section's content and size is to assign
a hexadecimal string to the `Content` field. Unfortunately this way is
completely useless when you need to declare a really large section.

To solve this problem this patch adds one more optional field `Size`
to the `RawContentSection` structure. When yaml2obj generates an ELF file
it uses the following algorithm:
1. If both `Content` and `Size` fields are missed create an empty section.
2. If only `Content` field is missed take section length from the `Size`
   field and fill the section by zero.
3. If only `Size` field is missed create a section using data from
   the `Content` field.
4. If both `Content` and `Size` fields are provided validate that the `Size`
   value is not less than size of `Content` data. Than take section length
   from the `Size`, fill beginning of the section by `Content` and the rest
   by zero.

Examples
--------
* Create a section 0x10000 bytes long filled by zero
  Name: .data
  Type: SHT_PROGBITS
  Flags: [ SHF_ALLOC ]
  Size: 0x10000

* Create a section 0x10000 bytes long starting from 'CA' 'FE' 'BA' 'BE'
  Name: .data
  Type: SHT_PROGBITS
  Flags: [ SHF_ALLOC ]
  Content: CAFEBABE
  Size: 0x10000

The patch reviewed by Michael Spencer.

llvm-svn: 208995
2014-05-16 16:01:00 +00:00
Simon Atanasyan ae6bb33ac2 [obj2yaml] Support ELF input format in the obj2yaml tool.
The ELF header e_flags field in the MIPS related test cases handled
incorrectly. The obj2yaml prints too many flags. I will fix that in the
next patches.

The patch reviewed by Michael Spencer and Sean Silva.

llvm-svn: 208752
2014-05-14 05:07:47 +00:00