Commit Graph

1783 Commits

Author SHA1 Message Date
Pengxuan Zheng 2e0c46044a [Object][COFF] Improve section name parsing
Inspired by discussions on D127369, we probably can further improve LLVM's COFF
section name parsing. Hopefully, this makes the logic simpler and handles some
edge cases more elegantly.

Reviewed By: rnk

Differential Revision: https://reviews.llvm.org/D127902
2022-06-16 17:01:10 -07:00
Juergen Ributzka 0557c2d589 [llvm] Fix MachO exports trie parsing.
The exports trie parser ordinal validation check doesn't consider the case where
the ordinal can be zero or negative for certain special values that are defined
in BindSpecialDylib. Update the validation to account for that fact and add a
test case.

This fixes rdar://94844233.

Differential Revision: https://reviews.llvm.org/D127806
2022-06-15 09:28:58 -07:00
Martin Storsjö b209b9e11c [COFF] Don't reject executables with data directories pointing outside of provided data
Before bb94611d65, we didn't check
that the sections in the COFF executable actually contained enough
raw data, when looking up what section contains tables pointed to
by the data directories.

That commit added checking, to avoid setting a pointer that points
out of bounds - by rejecting such executables.

It turns out that some binaries (e.g.g a "helper.exe" provided by
NSIS) contains a base relocation table data directory that points
into the wrong section. It points inside the virtual address space
allocated for that section, but the section contains much less raw
data, and the table points outside of the provided raw data.

No longer reject such binaries (to let tools operate on them and
inspect them), but don't set the table pointers (so that when
printing e.g. base relocations, we don't print anything).

This should fix the regression pointed out in
https://reviews.llvm.org/D126898#3565834.

Differential Revision: https://reviews.llvm.org/D127345
2022-06-15 16:51:20 +03:00
Pengxuan Zheng 064db24311 [Object][COFF] Fix section name parsing error when the name field is not null-padded
Some object files produced by Mirosoft tools contain sections whose name field
is not fully null-padded at the end. Microsoft's dumpbin is able to print the
section name correctly, but this causes parsing errors with LLVM tools.

So far, this issue only seems to happen when the section name is longer than 8
bytes. In this case, the section name field contains a slash (/) followed by the
offset into the string table, but the name field is not fully null-padded at the
end.

Reviewed By: mstorsjo

Differential Revision: https://reviews.llvm.org/D127369
2022-06-09 12:58:28 -07:00
Joseph Huber f06731e3c3 [Binary] Make the OffloadingImage type own the memory
Summary:
The OffloadingBinary uses a convenience struct to help manage the memory
that will be serialized using the binary format. This currently uses a
reference to an existing buffer, but this should own the memory instead
so it is easier to work with seeing as its only current use requires
saving the buffer anyway.
2022-06-07 15:56:09 -04:00
Joseph Huber 9db2f32336 [Binary] Align the image offset in OffloadBinary
Summary:
The OffloadBinary wraps around an embedded device image, commonly an ELF
or LLVM BC file. These file formats have alignment requirements for
parsing, so if the image is stored at an un-aligned offset from the
beginning of the file we will be unable to parse the embeded image
without copying the image buffer. This patch adds alignment padding
before the binary image is appended to ensure we can parse the symbolic
file it contains in-place without copying memory.
2022-06-07 15:56:08 -04:00
Derek Schuff 2ae385e560 [WebAssembly] Add WASM_SEC_LAST_KNOWN to BinaryFormat section types list [NFC]
There are 3 places where we were using WASM_SEC_TAG as the "last" known
section type, which requires updating (or leaves a bug) when a new known
section type is added. Instead add a "last type" to the enum for this
purpose.

Differential Revision: https://reviews.llvm.org/D127164
2022-06-07 12:05:23 -07:00
Fangrui Song 15d82c62dc [MC] De-capitalize MCStreamer functions
Follow-up to c031378ce0 .
The class is mostly consistent now.
2022-06-07 00:31:02 -07:00
Pengxuan Zheng c1d3cfea08 [Object][Archive] Support a new archive member /<ECSYMBOLS>/
Some libraries (e.g., arm64rt.lib) from the Windows WDK (version 10.0.22000.0)
contain an undocumented special member '/<ECSYMBOLS>/'. This causes llvm-lib to
fail with the following error:

"truncated or malformed archive (long name offset characters after the '/' are
not all decimal numbers: '<ECSYMBOLS>/' for archive member header at offset 162)"

The '/<ECSYMBOLS>/' member does not seem to be documented anywhere, but might be
related to the ARM64EC ABI Microsoft announced last year.

https://blogs.windows.com/windowsdeveloper/2021/06/28/announcing-arm64ec-building-native-and-interoperable-apps-for-windows-11-on-arm/

Reviewed By: thieta, thakis

Differential Revision: https://reviews.llvm.org/D127135
2022-06-06 17:20:56 -07:00
Chris Bieneman 21c9452305 [DX][ObjYAML] Support for parsing DXIL part
This patch adds support for parsing the DXIL part data into the
ObjectYAML tooling.

The DXIL part has additional headers describing the shader and bitcode
data and stores serialized bitcode after the headers.

Depends on D124945

Reviewed By: kuhar

Differential Revision: https://reviews.llvm.org/D126795
2022-06-06 18:46:19 -05:00
Chris Bieneman 352c395fb6 [ObjectYAML][DX] Add dxcontainer2yaml support
This change finishes fleshing out the ObjectYAML tools to support
converting DXContainer files into yaml representations.

Depends on D124944

Reviewed By: lhames

Differential Revision: https://reviews.llvm.org/D124945
2022-06-06 13:23:29 -05:00
Chris Bieneman 0498415f1d Fix overflow bug impacting 32-bit testing
This test was failing on 32-bit arm builders due to an interger
overflow. This changes the math to avoid overflow and should resolve
the test failure.
2022-06-06 11:16:16 -05:00
Benjamin Kramer 3a252806f4 [COFF] Fix -Wredundant-move 2022-06-03 17:39:08 +02:00
Alvin Wong bb94611d65 [COFF] Check table ptr more thoroughly and ignore empty sections
When loading split debug files for PE/COFF executables (produced with
`objcopy --only-keep-debug`), the tables or directories in such files
may point to data inside sections that may have been stripped.
COFFObjectFile shall detect and gracefully handle this, to allow the
object file be loaded without considering these tables or directories.
This is required for LLDB to load these files for use as debug symbols.

COFFObjectFile shall also check these pointers more carefully to account
for cases in which the section contains less raw data than the size
given by VirtualSize, to prevent going out of bounds.

This commit also changes COFFDump in llvm-objdump to reuse the pointers
that are already range-checked in COFFObjectFile. This fixes a crash
when trying to dump the TLS directory from a stripped file.

Fixes https://github.com/mstorsjo/llvm-mingw/issues/284

Reviewed By: rnk

Differential Revision: https://reviews.llvm.org/D126898
2022-06-03 18:31:01 +03:00
Fangrui Song 2108f7a243 [Object] Fix namespace style issues in D122069
https://llvm.org/docs/CodingStandards.html#use-namespace-qualifiers-to-implement-previously-declared-functions
2022-06-01 17:04:58 -07:00
Joseph Huber afd2f7e991 [Binary] Promote OffloadBinary to inherit from Binary
We use the `OffloadBinary` to create binary images of offloading files
and their corresonding metadata. This patch changes this to inherit from
the base `Binary` class. This allows us to create and insepect these
more generically. This patch includes all the necessary glue to
implement this as a new binary format, along with added the magic bytes
we use to distinguish the offloading binary to the `file_magic`
implementation.

Reviewed By: tra

Differential Revision: https://reviews.llvm.org/D126812
2022-06-01 18:40:57 -04:00
Chris Bieneman 9e3919dac4 [Object][DX] Parse DXContainer Parts
DXContainer files are structured as parts. This patch adds support for
parsing out the file part offsets and file part headers.

Reviewed By: kuhar

Differential Revision: https://reviews.llvm.org/D124804
2022-06-01 14:55:36 -05:00
Derek Schuff a205f2904d [WebAssembly] Consolidate sectionTypeToString in BinaryFormat [NFC]
Currently there are 2 duplicate implementation, and I want to add
a use in a 3rd place. Combine them in lib/BinaryFormat so they can
be shared.

Also update toString for symbol and reloc types to use StringRef

Differential Revision: https://reviews.llvm.org/D126553
2022-05-27 09:26:36 -07:00
Fangrui Song 9ee15bba47 [MC] Lower case the first letter of EmitCOFF* EmitWin* EmitCV*. NFC 2022-05-26 00:14:08 -07:00
Keith Smiley 066243057f
[Object] Fix updating darwin archives
When creating an archive, llvm-ar looks at the host to determine the
archive format to use, on Apple platforms this means it uses the
K_DARWIN format. K_DARWIN is _virtually_ equivalent to K_BSD, expect for
some very slight differences around padding, timestamps in deterministic
mode, and 64 bit formats. When updating an archive using llvm-ar, or
llvm-objcopy, Archive would try to determine the kind, but it was not
possible to get K_DARWIN in the initialization of the archive, because
they're virtually inciting usable from K_BSD, especially since the
slight differences only apply in very specific cases. This leads to
linker failures when the alignment workaround is not applied to an
archive copied with llvm-objcopy. This change teaches Archive to infer
the K_DARWIN type in the cases where it's possible and the first object
in the archive is a macho object. This avoids using the host triple to
determine this to not affect cross compiling.

Ideally we would eliminate the separate K_DARWIN type entirely since
it's not a truly separate archive type, but then we'd have to force the
macho workarounds on the BSD format generally. This might be acceptable
but then it would be unclear how to handle this case without forcing the
K_DARWIN64 format on all BSD users:

```
if (LastOffset >= Sym64Threshold) {
  if (Kind == object::Archive::K_DARWIN)
    Kind = object::Archive::K_DARWIN64;
  else
    Kind = object::Archive::K_GNU64;
}
```

The logic used to determine if the object is macho is derived from the
logic llvm-ar uses.

Previous context:

- 111cd669e9
- 23a76be5ad

Differential Revision: https://reviews.llvm.org/D124895
2022-05-19 10:56:26 -07:00
Chris Bieneman 47258ffc5c [NFC] correcting a code comment. 2022-05-18 08:53:23 -05:00
Rahman Lavaee 5f7ef65245 [llvm-objdump] Let --symbolize-operands symbolize basic block addresses based on the SHT_LLVM_BB_ADDR_MAP section.
`--symbolize-operands` already symbolizes branch targets based on the disassembly. When the object file is created with `-fbasic-block-sections=labels` (ELF-only) it will include a SHT_LLVM_BB_ADDR_MAP section which maps basic blocks to their addresses. In such case `llvm-objdump` can annotate the disassembly based on labels inferred on this section.

In contrast to the current labels, SHT_LLVM_BB_ADDR_MAP-based labels are created for every machine basic block including empty blocks and those which are not branched into (fallthrough blocks).

The old logic is still executed even when the SHT_LLVM_BB_ADDR_MAP section is present to handle functions which have not been received an entry in this section.

Reviewed By: jhenderson, MaskRay

Differential Revision: https://reviews.llvm.org/D124560
2022-05-16 10:11:11 -07:00
zhijian fe3b621f05 [AIX] support write operation of big archive.
SUMMARY

1. Enable supporting the write operation of big archive.
2. the first commit come from https://reviews.llvm.org/D104367
3. refactor the first commit and implement writing symbol table.
4. fixed the bugs and add more test cases in the second commit.

Reviewers: James Henderson
Differential Revision: https://reviews.llvm.org/D123949
2022-05-13 10:40:15 -04:00
Zi Xuan Wu (Zeson) 69f61d6e65 [Object] Support relocation resolver for CSKY
It enables relocation resolver for CSKY to pass some check-all test,
and also add some test about dwarf relocs.

Differential Revision: https://reviews.llvm.org/D125450
2022-05-13 13:38:17 +08:00
Chris Bieneman 19fb52cbfe [NFC] Add missing switch cases
This silences warnings encountered on some bots.
2022-05-02 18:13:07 -05:00
Chris Bieneman b26e44e623 [NFC] Fixing error on some versions of GCC
Some versions of GCC don't implicitly move Error to Expected.
2022-05-02 15:16:30 -05:00
Chris Bieneman 4070aa0156 [Object][DX] Initial DXContainer parsing support
This patch begins adding DXContainer parsing support to libObject.
Following the pattern used by ELFFile my goal here is to write a
standalone DXContainer parser and later write an adapter interface to
support a subset of the ObjectFile interfaces so that we can add
limited objdump support. I will also be adding ObjectYAML support to
help drive testing of the object tools and MC-level object writers as
those come together.

DXContainer is a slightly odd format. It is arranged in "parts" that
are semantically similar to sections, but it doesn't support symbol
listing.

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D124643
2022-05-02 13:56:33 -05:00
Joe Nash 813e521e55 [AMDGPU] Add gfx11 subtarget ELF definition
This is the first patch of a series to upstream support for the new
subtarget.

Contributors:
Jay Foad <jay.foad@amd.com>
Konstantin Zhuravlyov <kzhuravl_dev@outlook.com>

Patch 1/N for upstreaming AMDGPU gfx11 architectures.

Reviewed By: foad, kzhuravl, #amdgpu

Differential Revision: https://reviews.llvm.org/D124536
2022-04-29 12:27:17 -04:00
Ties Stuij 051deb2d9d [ARM] add Armv9 build attribute
The build attribute number can be found in the Arm ABI addenda32 document:
https://github.com/ARM-software/abi-aa/blob/main/addenda32/addenda32.rst#335target-related-attributes

Reviewed By: tmatheson

Differential Revision: https://reviews.llvm.org/D124090
2022-04-28 10:48:26 +01:00
Alexey Moksyakov 48e894a536 [BOLT] Add R_AARCH64_PREL16/32/64 relocations support
Reviewed By: yota9, rafauler

Differential Revision: https://reviews.llvm.org/D122294
2022-04-21 13:52:47 +03:00
Joseph Huber e471ba3d01 [Object] Add binary format for bundling offloading metadata
We need to embed certain metadata along with a binary image when we wish
to perform a device-linking job on it. Currently this metadata was
embedded in the section name of the data itself. This worked, but made
adding new metadata very difficult and didn't work if the user did any
sort of section linking.

This patch introduces a custom binary format for bundling offloading
metadata with a device object file. This binary format is fundamentally
a simple string map table with some additional data and an embedded
image. I decided to use a custom format rather than using an existing
format (ELF, JSON, etc) because of the specialty use-case of this. We
need a simple binary format that can be concatenated without requiring
other external dependencies.

This extension will make it easier to extend the linker wrapper's
capabilties with whatever data is necessary. Eventually this will allow
us to remove all the external arguments passed to the linker wrapper and
embed it directly in the host's linker so device linking behaves exactly
like host linking.

Reviewed By: JonChesterfield

Differential Revision: https://reviews.llvm.org/D122069
2022-04-14 10:50:52 -04:00
Kazu Hirata 5062d78f67 Apply clang-tidy fixes for readability-redundant-smartptr-get in TapiUniversal.cpp (NFC) 2022-03-28 09:18:36 -07:00
Kazu Hirata 7405bb82d3 Apply clang-tidy fixes for readability-redundant-smartptr-get in MachOUniversalWriter.cpp (NFC) 2022-03-28 09:18:35 -07:00
Fangrui Song 11a8fc6856 [llvm-objdump] --private-headers: change errors to warnings for dynamic section dumping
Fix #54456: `objcopy --only-keep-debug` produces a linked image with invalid
empty dynamic section. llvm-objdump -p currently reports an error which seems
excessive.

```
% llvm-readelf -l a.out
llvm-readelf: warning: 'a.out': no valid dynamic table was found
...
```

Follow the spirit of llvm-readelf -l (D64472) and report a warning instead.
This allows later files to be dumped despite warnings for an input file, and
improves objdump compatibility in that the exit code is now 0 instead of 1.

```
% llvm-objdump -p a.out  # new behavior
...
Program Header:
llvm-objdump: warning: 'a.out': invalid empty dynamic section
% objdump -p a.out
...
Dynamic Section:

```

Reviewed By: jhenderson, raj.khem

Differential Revision: https://reviews.llvm.org/D122505
2022-03-28 01:00:43 -07:00
Augusto Noronha 9b3af5e7b7 [dsymutil] Apply relocations present in Swift reflection sections
The strippable Swift reflection sections contain subtractor relocations
that need to be applied. There are two situations we need to support.
1) Both symbols used in the relocation come from the .o file (for
   example, one symbol lives in __swift5_fieldmd and the second in
   __swift5_reflstr).
2) One symbol comes from th .o file and the second from the main
   binary (for example, __swift5_fieldmd and __swift5_typeref).

Differential Revision: https://reviews.llvm.org/D120574
2022-03-17 14:23:20 -03:00
Sam Clegg 9b27fbd19c [WebAssembly] Fix asan issue from https://reviews.llvm.org/D121349 2022-03-15 11:36:56 -07:00
Sam Clegg 2481adb59c [WebAssembly] Fix asan issue from https://reviews.llvm.org/D121349 2022-03-14 19:57:50 -07:00
Joseph Huber 24ebdb6c25 [CUDA] Add CUDA fatbinary magic
Nvidia uses fatbinaries to bundle all of their device code. This patch
adds the magic number "0x50ed55ba" used in their propeitary format to
the list of magic identifies. This is technically undocumented and could
unlikely be changed by Nvidia in the future.

Reviewed By: tra

Differential Revision: https://reviews.llvm.org/D120932
2022-03-14 20:08:31 -04:00
Sam Clegg 9504ab32b7 [WebAssembly] Second phase of implemented extended const proposal
This change continues to lay the ground work for supporting extended
const expressions in the linker.

The included test covers object file reading and writing and the YAML
representation.

Differential Revision: https://reviews.llvm.org/D121349
2022-03-14 08:55:47 -07:00
serge-sans-paille ed98c1b376 Cleanup includes: DebugInfo & CodeGen
Discourse thread: https://discourse.llvm.org/t/include-what-you-use-include-cleanup
Differential Revision: https://reviews.llvm.org/D121332
2022-03-12 17:26:40 +01:00
Aakanksha 840695814a [AMDGPU] Add gfx1036 target
Differential Revision: https://reviews.llvm.org/D120846
2022-03-02 23:26:38 +00:00
Stanislav Mekhanoshin 2e2e64df4a [AMDGPU] Add gfx940 target
This is target definition only.

Differential Revision: https://reviews.llvm.org/D120688
2022-03-02 13:54:48 -08:00
Pavel Samolysov 2397f67166 [Object] Skip section offset checking for /<XFGHASHMAP>/
Starting from Windows SDK for Windows 11 (10.0.22000.x), all the system
libraries (.lib files) contain a section with the '/<XFGHASHMAP>/' name.
This looks like the libraries are built with control flow guard enabled:
https://docs.microsoft.com/en-us/cpp/build/reference/guard-enable-control-flow-guard?view=msvc-170

To let the LLVM tools (llvm-ar, llvm-lib) work with these libraries,
this patch just skips the section offset check for sections with the
'/<XFGHASHMAP>/' name.

Closes: llvm/llvm-project#53814

Signed-off-by: Pavel Samolysov <pavel.samolysov@intel.com>

Reviewed By: jhenderson, thieta

Differential Revision: https://reviews.llvm.org/D120645
2022-03-02 13:29:35 +02:00
Martin Storsjö 6ec18aafec [Object] [COFF] Improve error messages
This aids debugging when working with possibly broken files,
instead of just flat out erroring out without telling what's wrong.

Differential Revision: https://reviews.llvm.org/D120679
2022-03-02 10:44:41 +02:00
esmeyi 61835d19a8 [llvm-objcopy] Initial XCOFF32 support.
Summary: This is an initial implementation of lvm-objcopy for XCOFF32.
Currently only supports simple copying, op-passthrough to follow.

Reviewed By: jhenderson, shchenz

Differential Revision: https://reviews.llvm.org/D97656
2022-02-28 04:59:46 -05:00
Adrian Prantl bbaeb1ee0e Validate chained fixup image formats
This is part of a series of patches to upstream support for Mach-O
chained fixups.

Differential Revision: https://reviews.llvm.org/D113725
2022-02-25 13:04:00 -08:00
Arthur Eubanks 105ddd0fdc [NFC] Remove dead code (try 2)
This is causing
../../llvm/include/llvm/Object/MachO.h:379:13: warning: private field 'Kind' is not used [-Wunused-private-field]
  FixupKind Kind;

Previous attempt in a23f7c0cb6.
2022-02-22 16:15:01 -08:00
Adrian Prantl a3bfb01d94 Add support for chained fixup load commands to MachOObjectFile
This is part of a series of patches to upstream support for Mach-O chained fixups.

This patch adds support for parsing the chained fixup load command and
parsing the chained fixups header. It also puts into place the
abstract interface that will be used to iterate over the fixups.

Differential Revision: https://reviews.llvm.org/D113630
2022-02-22 11:06:27 -08:00
zhijian fd3ba1f862 Title: Export unique symbol list with llvm-nm new option "--export-symbols"
Summary:

the patch implement of following functionality.
1. export the symbols from archive or object files.
2. sort the export symbols. (based on same symbol name and visibility)
3. delete the duplicate export symbols (based on same symbol name and visibility)
4. print out the  unique and sorted export symbols (print the symbol name and visibility).

there are two new options are add in the patch
1. --export-symbols (enable the functionality of export unique symbol)
2. --no-rsrc (exclude the symbol name begin with "__rsrc" from be exporting from xcoff object file)

Export symbol list for xcoff object file has the same functionality as
The patch has the same functionality as
https://www.ibm.com/docs/en/xl-c-aix/13.1.0?topic=library-exporting-symbols-createexportlist-utility

Reviewers: James Henderson,Fangrui Song
Differential Revision: https://reviews.llvm.org/D112735
2022-02-17 11:37:33 -05:00
Keith Smiley a6e1b3c5c2 [ObjectYAML][MachO] Add LC_FUNCTION_STARTS support
This adds support for encoding and decoding the LC_FUNCTION_STARTS load
command payload.

Differential Revision: https://reviews.llvm.org/D119205
2022-02-13 09:24:47 -08:00