aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/MC/WinCOFFObjectWriter.cpp
AgeCommit message (Collapse)AuthorFilesLines
2023-06-05[NFC][COFF] Use COFFSection.MCSection when writeSectionHaohai Wen1-8/+11
Each COFFSection bind MCSection when created. No need to iterate throught MCAssembler when writeSection. Reviewed By: skan Differential Revision: https://reviews.llvm.org/D151793
2023-04-27Revert "[COFF] Add MC support for emitting IMAGE_WEAK_EXTERN_ANTI_DEPENDENCY ↵Zequan Wu1-2/+2
symbols" This reverts commit 10c17c97ebaf81ac26f6830e51a7a57ddcf63cd2. It causes undefined symbol error on chromium windows build. A small repro was uploaded to the code review.
2023-04-23[Coverity] Fix uninitialized scalar members in MCAkshay Khadse1-2/+2
This change fixes static code analysis errors Reviewed By: pengfei Differential Revision: https://reviews.llvm.org/D148814
2023-04-18Fix uninitialized pointer members in MCAkshay Khadse1-1/+1
Reviewed By: LuoYuanke Differential Revision: https://reviews.llvm.org/D148421
2023-04-17[COFF] Add MC support for emitting IMAGE_WEAK_EXTERN_ANTI_DEPENDENCY symbolsEli Friedman1-2/+2
This is mostly useful for ARM64EC, which uses such symbols extensively. One interesting quirk of ARM64EC is that we need to be able to emit weak symbols that point at each other (so if either symbol is defined elsewhere, both symbols point at the definition). This required a few changes to the way we handle weak symbols on Windows. Differential Revision: https://reviews.llvm.org/D145208
2023-04-13Revert "[COFF] Add MC support for emitting IMAGE_WEAK_EXTERN_ANTI_DEPENDENCY ↵Arthur Eubanks1-2/+2
symbols" This reverts commit fffdb7eac58b4efde5e23c1281e7a7f93a42d280. Causes crashes, see https://reviews.llvm.org/D145208
2023-04-07[COFF] Add MC support for emitting IMAGE_WEAK_EXTERN_ANTI_DEPENDENCY symbolsEli Friedman1-2/+2
This is mostly useful for ARM64EC, which uses such symbols extensively. One interesting quirk of ARM64EC is that we need to be able to emit weak symbols that point at each other (so if either symbol is defined elsewhere, both symbols point at the definition). This required a few changes to the way we handle weak symbols on Windows. Differential Revision: https://reviews.llvm.org/D145208
2023-03-15[llvm] Use *{Map,Set}::contains (NFC)Kazu Hirata1-4/+4
2023-01-05Move from llvm::makeArrayRef to ArrayRef deduction guides - llvm/ partserge-sans-paille1-1/+1
Use deduction guides instead of helper functions. The only non-automatic changes have been: 1. ArrayRef(some_uint8_pointer, 0) needs to be changed into ArrayRef(some_uint8_pointer, (size_t)0) to avoid an ambiguous call with ArrayRef((uint8_t*), (uint8_t*)) 2. CVSymbol sym(makeArrayRef(symStorage)); needed to be rewritten as CVSymbol sym{ArrayRef(symStorage)}; otherwise the compiler is confused and thinks we have a (bad) function prototype. There was a few similar situation across the codebase. 3. ADL doesn't seem to work the same for deduction-guides and functions, so at some point the llvm namespace must be explicitly stated. 4. The "reference mode" of makeArrayRef(ArrayRef<T> &) that acts as no-op is not supported (a constructor cannot achieve that). Per reviewers' comment, some useless makeArrayRef have been removed in the process. This is a follow-up to https://reviews.llvm.org/D140896 that introduced the deduction guides. Differential Revision: https://reviews.llvm.org/D140955
2022-11-24[reland][Alignment][NFC] Use the Align type in MCSectionGuillaume Chatelet1-1/+1
Differential Revision: https://reviews.llvm.org/D138653
2022-11-24Revert D138653 [Alignment][NFC] Use the Align type in MCSection"Guillaume Chatelet1-1/+1
This breaks the bolt project. This reverts commit 409f0dc4a420db1c6b259d5ae965a070c169d930.
2022-11-24[Alignment][NFC] Use the Align type in MCSectionGuillaume Chatelet1-1/+1
Differential Revision: https://reviews.llvm.org/D138653
2022-10-11[MC] .addrsig_sym: ignore unregistered symbolsFangrui Song1-0/+2
.addrsig_sym forces registering the symbol regardless whether it is otherwise registered. This creates an undefined symbol which is inconvenient/undesired: * `extern int x; void f() { (void)x; }` has inconsistent behavior whether `x` is emitted as an undefined symbol. `-O0 -faddrsig` makes `x` undefined while other -O levels and -fno-addrsig eliminate the symbol. * In ThinLTO, after a non-prevailing linkonce_odr definition is converted to available_externally, and then a declaration, the addrsig code emits a symbol while the symbol is otherwise unseen. D135427 fixed a bug that a non-prevailing `__cxx_global_var_init` was incorrectly retained. However, the IR declaration causes an undesired `.addrsig_sym __cxx_global_var_init`. This can be addressed in a way similar to D101512 (`isTransitiveUsedByMetadataOnly`) but the increased `OutStreamer->emitAddrsigSym(getSymbol(&GV));` complexity makes me nervous. Just ignoring unregistered symbols circumvents the problem. Reviewed By: rnk Differential Revision: https://reviews.llvm.org/D135642
2022-07-16[MC] Avoid UAF in WinCOFFObjectWriter with weak symbols.Tim Besard1-0/+1
When using weak symbols, the WinCOFFObjectWriter keeps a list (`WeakDefaults`) that's used to make names unique. This list should be reset when the object writer is reset, because otherwise reuse of the object writer can result in freed symbols being accessed. With some added output, this becomes clear when using `llc` in `--run-twice` mode: ``` $ ./llc --compile-twice -mtriple=x86_64-pc-win32 trivial.ll -filetype=obj DefineSymbol::WeakDefaults - .weak.foo.default - .weak.bar.default DefineSymbol::WeakDefaults - .weak.foo.default - áÑJij⌂ p§┼Ø┐☺ - .debug_macinfo.dw - .weak.bar.default ``` This does not seem to leak into the output object file though, so I couldn't come up with a test. I added one that just does `--run-twice` (and verified that it does access freed memory), which should result in detecting the invalid memory accesses when running under ASAN. Observed in a Julia PR where we started using weak symbols: https://github.com/JuliaLang/julia/pull/45649 Reviewed By: mstorsjo Differential Revision: https://reviews.llvm.org/D129840
2022-06-01[MC] [Win64EH] Check that the SEH unwind opcodes match the actual instructionsMartin Storsjö1-1/+1
It's a fairly common issue that the generating code incorrectly marks instructions as narrow or wide; check that the instruction lengths add up to the expected value, and error out if it doesn't. This allows catching code generation bugs. Also check that prologs and epilogs are properly terminated, to catch other code generation issues. Differential Revision: https://reviews.llvm.org/D125647
2022-05-03Implement support for __llvm_addrsig for MachO in llvm-mcAlex Borcan1-7/+0
The __llvm_addrsig section is a section that the linker needs for safe icf. This was not yet implemented for MachO - this is the implementation. It has been tested with a safe deduplication implementation inside lld. Reviewed By: MaskRay Differential Revision: https://reviews.llvm.org/D123751
2022-02-21[COFF] Move section name encoding into BinaryFormatNicolas Miller1-39/+2
Large COFF section names are moved into the string table and the section header field is the offset into the string table encoded in ASCII for offset smaller than 7 digits and in base64 for larger offsets. The operation of taking the string table offsets is done in a few places in the codebase, so it is helpful to move this operation into `BinaryFormat` so that it can be shared everywhere it's done. So this patch takes the implementation of this operation from `llvm/lib/MC/WinCOFFObjectWriter.cpp` and moves it into `BinaryFormat`. Reviewed By: jhenderson, rnk Differential Revision: https://reviews.llvm.org/D118793
2022-02-09Cleanup LLVMMC headersserge-sans-paille1-1/+0
There's a few relevant forward declarations in there that may require downstream adding explicit includes: llvm/MC/MCContext.h no longer includes llvm/BinaryFormat/ELF.h, llvm/MC/MCSubtargetInfo.h, llvm/MC/MCTargetOptions.h llvm/MC/MCObjectStreamer.h no longer include llvm/MC/MCAssembler.h llvm/MC/MCAssembler.h no longer includes llvm/MC/MCFixup.h, llvm/MC/MCFragment.h Counting preprocessed lines required to rebuild llvm-project on my setup: before: 1052436830 after: 1049293745 Which is significant and backs up the change in addition to the usual benefits of decreasing coupling between headers and compilation units. Discourse thread: https://discourse.llvm.org/t/include-what-you-use-include-cleanup Differential Revision: https://reviews.llvm.org/D119244
2021-11-23[COFF] [ARM64] Create symbols with regular intervals for relocations against ↵Martin Storsjö1-4/+44
temporary symbols For relocations against temporary symbols (that don't persist in the object file), we normally adjust them to reference the start of the section. For adrp relocations, the immediate offset from the referenced symbol is stored in the opcode as the 21 bit signed immediate; this means that the symbol referenced must be within +/- 1 MB from the referenced symbol. Create label symbols with regular intervals (1 MB intervals). For relocations against temporary symbols, pick the preceding added offset symbol and make the relocation against that instead of against the start of the section. This should fix the root issue behind https://bugs.llvm.org/show_bug.cgi?id=52378. Differential Revision: https://reviews.llvm.org/D114340
2021-05-12[COFF] Fix ARM and ARM64 REL32 relocations to be relative to the end of the ↵Martin Storsjö1-3/+7
relocation This matches how they are defined on X86. This should fix the relative lookup tables pass for COFF, allowing it to be reenabled. Differential Revision: https://reviews.llvm.org/D102217
2021-02-07ELFObjectWriter: Make STT_FILE precede associated local symbolsFangrui Song1-1/+2
2020-09-28[COFF] Aliases resolve directly to defined external targetsEric Astor1-2/+3
Avoid introducing unnecessary indirection for weak-external references. We only need to introduce ".weak.<SYMBOL>.default" when referencing a symbol that is defined, but not external. Reviewed By: mstorsjo Differential Revision: https://reviews.llvm.org/D88305
2020-07-24[MC] [COFF] Make sure that weak external symbols are undefined symbolsMartin Storsjö1-0/+1
For comdats (e.g. caused by -ffunction-sections), Section is already set here; make sure it's null, for the weak external symbol to be undefined. This fixes PR46779. Differential Revision: https://reviews.llvm.org/D84507
2020-07-11[COFF] Fix endianness of .llvm.call-graph-profile section dataZequan Wu1-3/+3
2020-07-10[COFF] Add cg_profile directive and .llvm.call-graph-profile sectionZequan Wu1-0/+23
Subscribers: hiraditya, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D83597
2020-04-17MCObjectWriter.h - remove Endian.h/EndianStream.h/raw_ostream.h includes. NFCSimon Pilgrim1-1/+1
Push these includes down to the the writers that actually need them, a number of which were implicitly relying on the MCObjectWriter.h.
2020-04-15[MC] Rename MCSection*::getSectionName() to getName(). NFCFangrui Song1-3/+3
A pending change will merge MCSection*::getName() to MCSection::getName().
2020-03-13[COFF] Assign unique names to autogenerated .weak.<name>.default symbolsMartin Storsjö1-0/+49
These symbols need to be external (MSVC tools error out if a weak external points at a symbol that isn't external; this was tried before but had to be reverted in bc5b7217dceecd3eec69593026a9e38dfbfd6908, and this was originally explicitly fixed in 732eeaf2a930ad2755cb4eb5d99a3deae0de4a72). If multiple object files have weak symbols with defaults, their defaults could cause linker errors due to duplicate definitions, unless the names of the defaults are unique. GNU binutils handles this by appending the name of another symbol from the same object file to the name of the default symbol. Try to implement something similar; before writing the object file, locate a symbol that should have a unique name and use the name of that one for making the weak defaults unique. Differential Revision: https://reviews.llvm.org/D75989
2020-01-28Make llvm::StringRef to std::string conversions explicit.Benjamin Kramer1-1/+1
This is how it should've been and brings it more in line with std::string_view. There should be no functional change here. This is mostly mechanical from a custom clang-tidy check, with a lot of manual fixups. It uncovers a lot of minor inefficiencies. This doesn't actually modify StringRef yet, I'll do that in a follow-up.
2019-12-28Revert "[COFF] Make the autogenerated .weak.<name>.default symbols static"Martin Storsjö1-13/+9
This reverts commit 7ca86ee6494d4307333b300bae80e42df4a5140f. Apparently this change causes MS link.exe to error out with "LNK1235: corrupt or invalid COFF symbol table".
2019-12-28[COFF] Make the autogenerated .weak.<name>.default symbols staticMartin Storsjö1-9/+13
If we have references to the same extern_weak in multiple objects, all of them would generate external symbols with the same name. Make them static to avoid duplicate definitions; nothing should need to refer to this symbol outside of the current object. GCC/binutils seems to handle the same by not using a fixed string for the ".default" suffix, but instead using the name of some other defined external symbol from the same object (which is supposed to be unique among objects unless there's other duplicate definitions). Differential Revision: https://reviews.llvm.org/D71711
2019-10-09Unify the two CRC implementationsHans Wennborg1-2/+2
David added the JamCRC implementation in r246590. More recently, Eugene added a CRC-32 implementation in r357901, which falls back to zlib's crc32 function if present. These checksums are essentially the same, so having multiple implementations seems unnecessary. This replaces the CRC-32 implementation with the simpler one from JamCRC, and implements the JamCRC interface in terms of CRC-32 since this means it can use zlib's implementation when available, saving a few bytes and potentially making it faster. JamCRC took an ArrayRef<char> argument, and CRC-32 took a StringRef. This patch changes it to ArrayRef<uint8_t> which I think is the best choice, and simplifies a few of the callers nicely. Differential revision: https://reviews.llvm.org/D68570 llvm-svn: 374148
2019-08-15[llvm] Migrate llvm::make_unique to std::make_uniqueJonas Devlieghere1-3/+3
Now that we've moved to C++14, we no longer need the llvm::make_unique implementation from STLExtras.h. This patch is a mechanical replacement of (hopefully) all the llvm::make_unique instances across the monorepo. llvm-svn: 369013
2019-06-26MC: correct the emission of weak aliases in COFFSaleem Abdulrasool1-1/+1
The weak alias should have the characteristics set to `IMAGE_EXTERN_WEAK_SEARCH_ALIAS` to indicate that the weak external here is a symbol alias and that the symbol is aliased to a locally defined symbol. We were previously setting the characteristics to `IMAGE_EXTERN_WEAK_SEARCH_LIBRARY` which indicates that the symbol should be looked for in the libraries. llvm-svn: 364370
2019-01-19Update the file headers across all of the LLVM projects in the monorepoChandler Carruth1-4/+3
to reflect the new license. We understand that people may be surprised that we're moving the header entirely to discuss the new license. We checked this carefully with the Foundation's lawyer and we believe this is the correct approach. Essentially, all code in the project is now made available by the LLVM project under our new license, so you will see that the license headers include that license only. Some of our contributors have contributed code under our old license, and accordingly, we have retained a copy of our old license notice in the top-level files in each project and repository. llvm-svn: 351636
2018-09-27llvm::sort(C.begin(), C.end(), ...) -> llvm::sort(C, ...)Fangrui Song1-4/+3
Summary: The convenience wrapper in STLExtras is available since rL342102. Reviewers: dblaikie, javed.absar, JDevlieghere, andreadb Subscribers: MatzeB, sanjoy, arsenm, dschuff, mehdi_amini, sdardis, nemanjai, jvesely, nhaehnle, sbc100, jgravelle-google, eraman, aheejin, kbarton, JDevlieghere, javed.absar, gbedwell, jrtc27, mgrang, atanasyan, steven_wu, george.burgess.iv, dexonsmith, kristina, jsji, llvm-commits Differential Revision: https://reviews.llvm.org/D52573 llvm-svn: 343163
2018-08-23Initialize the address-significance table fragment's layout order.Peter Collingbourne1-0/+1
This fragment is created after layout, which is where the order normally gets set. Should fix a test failure under msan. llvm-svn: 340516
2018-08-23MC: Don't align COFF section contents.Peter Collingbourne1-9/+1
Aligning section contents is not required, but only recommended, by the specification. Microsoft's documentation says (https://docs.microsoft.com/en-us/windows/desktop/debug/pe-format#section-table-section-headers): "For object files, the value should be aligned on a 4-byte boundary for best performance." However, according to my measurements, aligning section contents has a neutral to negative effect on performance. I measured the median run time of 100 links of Chromium's base_unittests on Linux with lld-link and on Windows with link.exe with both aligned and unaligned sections. On Linux I didn't see a measurable performance difference, and on Windows the link was slightly faster with unaligned sections (presumably because on Windows the bottleneck is I/O). Also, the sections created by cl.exe are unaligned, so we should expect tools to broadly accept unaligned sections. Differential Revision: https://reviews.llvm.org/D51149 llvm-svn: 340514
2018-08-22MC: Teach the COFF object writer to write address-significance tables.Peter Collingbourne1-0/+35
The format is the same as in ELF: a sequence of ULEB128-encoded symbol indexes. Differential Revision: https://reviews.llvm.org/D51047 llvm-svn: 340499
2018-08-18MC: Remove dead code from WinCOFFObjectWriter.cpp. NFCI.Peter Collingbourne1-20/+0
Remove code for writing auxiliary symbols of type function definition and begin function. These types of symbols are associated with pre-CodeView debug info and we never emit them. llvm-svn: 340113
2018-08-16[MC] Improve COFF associative section lookupReid Kleckner1-11/+17
Handle the case when the symbol is private. Private symbols are not in the COFF object file symbol table, so they aren't inserted into SymbolMap. We can't look up the section of the symbol that way. Instead, get the MCSection from the MCSymbol and map that to the object file section. Print a better error message when the symbol has no section, like when the symbol is undefined. Fixes PR38607 llvm-svn: 339942
2018-05-21MC: Remove stream and output functions from MCObjectWriter. NFCI.Peter Collingbourne1-2/+1
Part of PR37466. Differential Revision: https://reviews.llvm.org/D47043 llvm-svn: 332864
2018-05-21MC: Have the object writers return the number of bytes written. NFCI.Peter Collingbourne1-3/+7
This removes the last external use of the stream. Part of PR37466. Differential Revision: https://reviews.llvm.org/D47042 llvm-svn: 332863
2018-05-21MC: Change object writers to use endian::Writer. NFCI.Peter Collingbourne1-77/+79
Part of PR37466. Differential Revision: https://reviews.llvm.org/D47040 llvm-svn: 332861
2018-05-21MC: Change MCAssembler::writeSectionData and writeFragmentPadding to take a ↵Peter Collingbourne1-9/+1
raw_ostream. NFCI. Also clean up a couple of hacks where we were writing the section contents to another stream by setting the object writer's stream, writing and setting it back. Part of PR37466. Differential Revision: https://reviews.llvm.org/D47038 llvm-svn: 332858
2018-04-13[MC] Change std::sort to llvm::sort in response to r327219Mandeep Singh Grang1-4/+4
Summary: r327219 added wrappers to std::sort which randomly shuffle the container before sorting. This will help in uncovering non-determinism caused due to undefined sorting order of objects having the same key. To make use of that infrastructure we need to invoke llvm::sort instead of std::sort. Note: This patch is one of a series of patches to replace *all* std::sort to llvm::sort. Refer the comments section in D44363 for a list of all the required patches. Reviewers: grosbach, void, ruiu Reviewed By: ruiu Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D45138 llvm-svn: 330058
2018-03-14[MC] Always emit relocations for same-section function referencesReid Kleckner1-5/+7
Summary: We already emit relocations in this case when the "incremental linker compatible" flag is set, but it turns out these relocations are also required for /guard:cf. Now that we have two use cases for this behavior, let's make it unconditional to try to keep things simple. We never hit this problem in Clang because it always sets the "incremental linker compatible" flag when targeting MSVC. However, LLD LTO doesn't set this flag, so we'd get CFG failures at runtime when using ThinLTO and /guard:cf. We probably don't want LLD LTO to set the "incremental linker compatible" assembler flag, since this has nothing to do with incremental linking, and we don't need to timestamp LTO temporary objects. Fixes PR36624. Reviewers: inglorion, espindola, majnemer Subscribers: mehdi_amini, llvm-commits, hiraditya Differential Revision: https://reviews.llvm.org/D44485 llvm-svn: 327557
2017-10-10[MC] Thread unique_ptr<MCObjectWriter> through the create.*ObjectWriterLang Hames1-2/+2
functions. This makes the ownership of the resulting MCObjectWriter clear, and allows us to remove one instance of MCObjectStreamer's bizarre "holding ownership via someone else's reference" trick. llvm-svn: 315327
2017-10-10[MC] Plumb unique_ptr<MCWinCOFFObjectTargetWriter> throughLang Hames1-8/+8
createWinCOFFObjectWriter to WinCOFFObjectWriter's constructor. Fixes the same ownership issue for COFF that r315245 did for MachO: WinCOFFObjectWriter takes ownership of its MCWinCOFFObjectTargetWriter, so we want to pass this through to the constructor via a unique_ptr, rather than a raw ptr. llvm-svn: 315257
2017-07-11Simplify interface now that we don't need to pass IsPCRel. NFC.Rafael Espindola1-5/+6
llvm-svn: 307734