aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/MC/MachObjectWriter.cpp
diff options
context:
space:
mode:
authorAlex Borcan <alexborcan@fb.com>2022-05-03 18:19:18 -0400
committerJez Ng <jezng@fb.com>2022-05-03 18:19:18 -0400
commitafaa56df7a93f21cd1e49f7daa6a8970335744da (patch)
treefb926c289d2de4101a6e408268f0bd3d1bf7028b /llvm/lib/MC/MachObjectWriter.cpp
parentb945b62cf35e1b45ffb9233958756743b2b5fd46 (diff)
downloadllvm-afaa56df7a93f21cd1e49f7daa6a8970335744da.zip
llvm-afaa56df7a93f21cd1e49f7daa6a8970335744da.tar.gz
llvm-afaa56df7a93f21cd1e49f7daa6a8970335744da.tar.bz2
Implement support for __llvm_addrsig for MachO in llvm-mc
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
Diffstat (limited to 'llvm/lib/MC/MachObjectWriter.cpp')
-rw-r--r--llvm/lib/MC/MachObjectWriter.cpp21
1 files changed, 21 insertions, 0 deletions
diff --git a/llvm/lib/MC/MachObjectWriter.cpp b/llvm/lib/MC/MachObjectWriter.cpp
index 56bb03a..173755d 100644
--- a/llvm/lib/MC/MachObjectWriter.cpp
+++ b/llvm/lib/MC/MachObjectWriter.cpp
@@ -19,6 +19,7 @@
#include "llvm/MC/MCFixupKindInfo.h"
#include "llvm/MC/MCFragment.h"
#include "llvm/MC/MCMachObjectWriter.h"
+#include "llvm/MC/MCObjectFileInfo.h"
#include "llvm/MC/MCObjectWriter.h"
#include "llvm/MC/MCSection.h"
#include "llvm/MC/MCSectionMachO.h"
@@ -29,6 +30,7 @@
#include "llvm/Support/Casting.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
+#include "llvm/Support/LEB128.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
@@ -751,6 +753,24 @@ static MachO::LoadCommandType getLCFromMCVM(MCVersionMinType Type) {
llvm_unreachable("Invalid mc version min type");
}
+// Encode addrsig data as symbol indexes in variable length encoding.
+void MachObjectWriter::writeAddrsigSection(MCAssembler &Asm) {
+ MCSection *AddrSigSection =
+ Asm.getContext().getObjectFileInfo()->getAddrSigSection();
+ MCSection::FragmentListType &fragmentList = AddrSigSection->getFragmentList();
+ if (!fragmentList.size())
+ return;
+
+ assert(fragmentList.size() == 1);
+ MCFragment *pFragment = &*fragmentList.begin();
+ MCDataFragment *pDataFragment = dyn_cast_or_null<MCDataFragment>(pFragment);
+ assert(pDataFragment);
+
+ raw_svector_ostream OS(pDataFragment->getContents());
+ for (const MCSymbol *sym : this->getAddrsigSyms())
+ encodeULEB128(sym->getIndex(), OS);
+}
+
uint64_t MachObjectWriter::writeObject(MCAssembler &Asm,
const MCAsmLayout &Layout) {
uint64_t StartOffset = W.OS.tell();
@@ -758,6 +778,7 @@ uint64_t MachObjectWriter::writeObject(MCAssembler &Asm,
// Compute symbol table information and bind symbol indices.
computeSymbolTable(Asm, LocalSymbolData, ExternalSymbolData,
UndefinedSymbolData);
+ writeAddrsigSection(Asm);
if (!Asm.CGProfile.empty()) {
MCSection *CGProfileSection = Asm.getContext().getMachOSection(