Skip to content
This repository was archived by the owner on Dec 20, 2019. It is now read-only.

Commit 66a0a02

Browse files
committed
LDC: Apply Joakim's Android patches for respective targets
1 parent de51970 commit 66a0a02

File tree

10 files changed

+89
-28
lines changed

10 files changed

+89
-28
lines changed

lib/CodeGen/TargetLoweringObjectFileImpl.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,9 @@ const MCExpr *TargetLoweringObjectFileELF::getTTypeGlobalReference(
232232
MMI, Streamer);
233233
}
234234

235-
static SectionKind getELFKindForNamedSection(StringRef Name, SectionKind K) {
236-
// N.B.: The defaults used in here are not the same ones used in MC.
235+
static SectionKind getELFKindForNamedSection(StringRef Name, SectionKind K,
236+
const Triple &TargetTriple) {
237+
// N.B.: The defaults used in here are no the same ones used in MC.
237238
// We follow gcc, MC follows gas. For example, given ".section .eh_frame",
238239
// both gas and MC will produce a section with no flags. Given
239240
// section(".eh_frame") gcc will produce:
@@ -264,6 +265,7 @@ static SectionKind getELFKindForNamedSection(StringRef Name, SectionKind K) {
264265
return SectionKind::getThreadData();
265266

266267
if (Name == ".tbss" ||
268+
(TargetTriple.isAndroid() && Name == ".tcommon") || // LDC
267269
Name.startswith(".tbss.") ||
268270
Name.startswith(".gnu.linkonce.tb.") ||
269271
Name.startswith(".llvm.linkonce.tb."))
@@ -294,7 +296,7 @@ static unsigned getELFSectionType(StringRef Name, SectionKind K) {
294296
return ELF::SHT_PROGBITS;
295297
}
296298

297-
static unsigned getELFSectionFlags(SectionKind K) {
299+
static unsigned getELFSectionFlags(SectionKind K, const Triple &TargetTriple) {
298300
unsigned Flags = 0;
299301

300302
if (!K.isMetadata())
@@ -309,7 +311,7 @@ static unsigned getELFSectionFlags(SectionKind K) {
309311
if (K.isWriteable())
310312
Flags |= ELF::SHF_WRITE;
311313

312-
if (K.isThreadLocal())
314+
if (K.isThreadLocal() && !TargetTriple.isAndroid()) // LDC
313315
Flags |= ELF::SHF_TLS;
314316

315317
if (K.isMergeableCString() || K.isMergeableConst())
@@ -375,10 +377,10 @@ MCSection *TargetLoweringObjectFileELF::getExplicitSectionGlobal(
375377
}
376378

377379
// Infer section flags from the section name if we can.
378-
Kind = getELFKindForNamedSection(SectionName, Kind);
380+
Kind = getELFKindForNamedSection(SectionName, Kind, getTargetTriple());
379381

380382
StringRef Group = "";
381-
unsigned Flags = getELFSectionFlags(Kind);
383+
unsigned Flags = getELFSectionFlags(Kind, getTargetTriple());
382384
if (const Comdat *C = getELFComdat(GO)) {
383385
Group = C->getName();
384386
Flags |= ELF::SHF_GROUP;
@@ -502,7 +504,7 @@ static MCSectionELF *selectELFSectionForGlobal(
502504

503505
MCSection *TargetLoweringObjectFileELF::SelectSectionForGlobal(
504506
const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
505-
unsigned Flags = getELFSectionFlags(Kind);
507+
unsigned Flags = getELFSectionFlags(Kind, getTargetTriple());
506508

507509
// If we have -ffunction-section or -fdata-section then we should emit the
508510
// global value to a uniqued section specifically for it.

lib/MC/MCELFStreamer.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,12 @@ void MCELFStreamer::fixSymbolsInTLSFixups(const MCExpr *expr) {
448448
break;
449449
}
450450
getAssembler().registerSymbol(symRef.getSymbol());
451-
cast<MCSymbolELF>(symRef.getSymbol()).setType(ELF::STT_TLS);
451+
// LDC
452+
{
453+
auto ofi = getContext().getObjectFileInfo();
454+
if (!(ofi && ofi->getTargetTriple().isAndroid()))
455+
cast<MCSymbolELF>(symRef.getSymbol()).setType(ELF::STT_TLS);
456+
}
452457
break;
453458
}
454459

lib/MC/MCObjectFileInfo.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -489,12 +489,14 @@ void MCObjectFileInfo::initELFMCObjectFileInfo(const Triple &T, bool Large) {
489489
ReadOnlySection =
490490
Ctx->getELFSection(".rodata", ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
491491

492-
TLSDataSection =
493-
Ctx->getELFSection(".tdata", ELF::SHT_PROGBITS,
494-
ELF::SHF_ALLOC | ELF::SHF_TLS | ELF::SHF_WRITE);
492+
// LDC
493+
const auto tlsFlag = (!getTargetTriple().isAndroid() ? ELF::SHF_TLS : 0);
495494

496-
TLSBSSSection = Ctx->getELFSection(
497-
".tbss", ELF::SHT_NOBITS, ELF::SHF_ALLOC | ELF::SHF_TLS | ELF::SHF_WRITE);
495+
TLSDataSection = Ctx->getELFSection(
496+
".tdata", ELF::SHT_PROGBITS, ELF::SHF_ALLOC | tlsFlag | ELF::SHF_WRITE);
497+
498+
TLSBSSSection = Ctx->getELFSection(".tbss", ELF::SHT_NOBITS,
499+
ELF::SHF_ALLOC | tlsFlag | ELF::SHF_WRITE);
498500

499501
DataRelROSection = Ctx->getELFSection(".data.rel.ro", ELF::SHT_PROGBITS,
500502
ELF::SHF_ALLOC | ELF::SHF_WRITE);

lib/Target/AArch64/AArch64ISelLowering.cpp

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4149,6 +4149,31 @@ AArch64TargetLowering::LowerELFGlobalTLSAddress(SDValue Op,
41494149
return DAG.getNode(ISD::ADD, DL, PtrVT, ThreadBase, TPOff);
41504150
}
41514151

4152+
SDValue
4153+
AArch64TargetLowering::LowerAndroidGlobalTLSAddress(SDValue Op,
4154+
SelectionDAG &DAG) const {
4155+
assert(Subtarget->isTargetELF() && "This function expects an ELF target");
4156+
SDLoc DL(Op);
4157+
SDValue Result = LowerGlobalAddress(Op, DAG);
4158+
SDValue Chain = DAG.getEntryNode();
4159+
ArgListTy Args;
4160+
ArgListEntry Entry;
4161+
Type *Ty = (Type *)Type::getInt64Ty(*DAG.getContext());
4162+
Entry.Node = Result;
4163+
Entry.Ty = Ty;
4164+
Args.push_back(Entry);
4165+
4166+
// copied, modified from ARMTargetLowering::LowerToTLSGeneralDynamicModel
4167+
TargetLowering::CallLoweringInfo CLI(DAG);
4168+
CLI.setDebugLoc(DL).setChain(Chain).setLibCallee(
4169+
CallingConv::C, Ty,
4170+
DAG.getExternalSymbol("__tls_get_addr",
4171+
getPointerTy(DAG.getDataLayout())),
4172+
std::move(Args));
4173+
std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
4174+
return CallResult.first;
4175+
}
4176+
41524177
SDValue
41534178
AArch64TargetLowering::LowerWindowsGlobalTLSAddress(SDValue Op,
41544179
SelectionDAG &DAG) const {
@@ -4216,8 +4241,12 @@ SDValue AArch64TargetLowering::LowerGlobalTLSAddress(SDValue Op,
42164241

42174242
if (Subtarget->isTargetDarwin())
42184243
return LowerDarwinGlobalTLSAddress(Op, DAG);
4219-
if (Subtarget->isTargetELF())
4220-
return LowerELFGlobalTLSAddress(Op, DAG);
4244+
if (Subtarget->isTargetELF()) {
4245+
if (Subtarget->isTargetAndroid())
4246+
return LowerAndroidGlobalTLSAddress(Op, DAG); // LDC
4247+
else
4248+
return LowerELFGlobalTLSAddress(Op, DAG);
4249+
}
42214250
if (Subtarget->isTargetWindows())
42224251
return LowerWindowsGlobalTLSAddress(Op, DAG);
42234252

lib/Target/AArch64/AArch64ISelLowering.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,7 @@ class AArch64TargetLowering : public TargetLowering {
589589
SDValue LowerADDROFRETURNADDR(SDValue Op, SelectionDAG &DAG) const;
590590
SDValue LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const;
591591
SDValue LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const;
592+
SDValue LowerAndroidGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const; // LDC
592593
SDValue LowerDarwinGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const;
593594
SDValue LowerELFGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const;
594595
SDValue LowerELFTLSDescCallSeq(SDValue SymAddr, const SDLoc &DL,

lib/Target/AArch64/MCTargetDesc/AArch64MCExpr.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
//===----------------------------------------------------------------------===//
1414

1515
#include "AArch64MCExpr.h"
16+
#include "llvm/MC/MCAssembler.h" // LDC
1617
#include "llvm/MC/MCContext.h"
18+
#include "llvm/MC/MCObjectFileInfo.h" // LDC
1719
#include "llvm/MC/MCStreamer.h"
1820
#include "llvm/MC/MCSymbolELF.h"
1921
#include "llvm/MC/MCValue.h"
@@ -122,7 +124,12 @@ static void fixELFSymbolsInTLSFixupsImpl(const MCExpr *Expr, MCAssembler &Asm) {
122124
// We're known to be under a TLS fixup, so any symbol should be
123125
// modified. There should be only one.
124126
const MCSymbolRefExpr &SymRef = *cast<MCSymbolRefExpr>(Expr);
125-
cast<MCSymbolELF>(SymRef.getSymbol()).setType(ELF::STT_TLS);
127+
// LDC
128+
{
129+
auto ofi = Asm.getContext().getObjectFileInfo();
130+
if (!(ofi && ofi->getTargetTriple().isAndroid()))
131+
cast<MCSymbolELF>(SymRef.getSymbol()).setType(ELF::STT_TLS);
132+
}
126133
break;
127134
}
128135

lib/Target/ARM/MCTargetDesc/ARMAsmBackendELF.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,19 @@ using namespace llvm;
1818

1919
namespace {
2020
class ARMAsmBackendELF : public ARMAsmBackend {
21+
// LDC
22+
const bool isAndroid;
23+
2124
public:
2225
uint8_t OSABI;
2326
ARMAsmBackendELF(const Target &T, const MCSubtargetInfo &STI, uint8_t OSABI,
2427
support::endianness Endian)
25-
: ARMAsmBackend(T, STI, Endian), OSABI(OSABI) {}
28+
: ARMAsmBackend(T, STI, Endian),
29+
isAndroid(STI.getTargetTriple().isAndroid()), OSABI(OSABI) {}
2630

2731
std::unique_ptr<MCObjectTargetWriter>
2832
createObjectTargetWriter() const override {
29-
return createARMELFObjectWriter(OSABI);
33+
return createARMELFObjectWriter(OSABI, isAndroid);
3034
}
3135
};
3236
}

lib/Target/ARM/MCTargetDesc/ARMELFObjectWriter.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,16 @@ using namespace llvm;
2525
namespace {
2626

2727
class ARMELFObjectWriter : public MCELFObjectTargetWriter {
28+
// LDC
29+
const bool isAndroid;
30+
2831
enum { DefaultEABIVersion = 0x05000000U };
2932

3033
unsigned GetRelocTypeInner(const MCValue &Target, const MCFixup &Fixup,
3134
bool IsPCRel, MCContext &Ctx) const;
3235

3336
public:
34-
ARMELFObjectWriter(uint8_t OSABI);
37+
ARMELFObjectWriter(uint8_t OSABI, bool IsAndroid);
3538

3639
~ARMELFObjectWriter() override = default;
3740

@@ -44,10 +47,10 @@ namespace {
4447

4548
} // end anonymous namespace
4649

47-
ARMELFObjectWriter::ARMELFObjectWriter(uint8_t OSABI)
48-
: MCELFObjectTargetWriter(/*Is64Bit*/ false, OSABI,
49-
ELF::EM_ARM,
50-
/*HasRelocationAddend*/ false) {}
50+
ARMELFObjectWriter::ARMELFObjectWriter(uint8_t OSABI, bool IsAndroid)
51+
: MCELFObjectTargetWriter(/*Is64Bit*/ false, OSABI, ELF::EM_ARM,
52+
/*HasRelocationAddend*/ false),
53+
isAndroid(IsAndroid) {}
5154

5255
bool ARMELFObjectWriter::needsRelocateWithSymbol(const MCSymbol &Sym,
5356
unsigned Type) const {
@@ -164,7 +167,8 @@ unsigned ARMELFObjectWriter::GetRelocTypeInner(const MCValue &Target,
164167
case MCSymbolRefExpr::VK_GOT:
165168
return ELF::R_ARM_GOT_BREL;
166169
case MCSymbolRefExpr::VK_TLSGD:
167-
return ELF::R_ARM_TLS_GD32;
170+
// LDC
171+
return isAndroid ? ELF::R_ARM_GOT_PREL : ELF::R_ARM_TLS_GD32;
168172
case MCSymbolRefExpr::VK_TPOFF:
169173
return ELF::R_ARM_TLS_LE32;
170174
case MCSymbolRefExpr::VK_GOTTPOFF:
@@ -237,6 +241,6 @@ unsigned ARMELFObjectWriter::GetRelocTypeInner(const MCValue &Target,
237241
}
238242

239243
std::unique_ptr<MCObjectTargetWriter>
240-
llvm::createARMELFObjectWriter(uint8_t OSABI) {
241-
return llvm::make_unique<ARMELFObjectWriter>(OSABI);
244+
llvm::createARMELFObjectWriter(uint8_t OSABI, bool IsAndroid) {
245+
return llvm::make_unique<ARMELFObjectWriter>(OSABI, IsAndroid);
242246
}

lib/Target/ARM/MCTargetDesc/ARMMCTargetDesc.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ MCStreamer *createARMWinCOFFStreamer(MCContext &Context,
8787
bool IncrementalLinkerCompatible);
8888

8989
/// Construct an ELF Mach-O object writer.
90-
std::unique_ptr<MCObjectTargetWriter> createARMELFObjectWriter(uint8_t OSABI);
90+
std::unique_ptr<MCObjectTargetWriter> createARMELFObjectWriter(uint8_t OSABI,
91+
bool IsAndroid);
9192

9293
/// Construct an ARM Mach-O object writer.
9394
std::unique_ptr<MCObjectTargetWriter>

lib/Target/X86/MCTargetDesc/X86ELFObjectWriter.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "llvm/MC/MCELFObjectWriter.h"
1616
#include "llvm/MC/MCExpr.h"
1717
#include "llvm/MC/MCFixup.h"
18+
#include "llvm/MC/MCObjectFileInfo.h" // LDC
1819
#include "llvm/MC/MCObjectWriter.h"
1920
#include "llvm/MC/MCValue.h"
2021
#include "llvm/Support/ErrorHandling.h"
@@ -260,7 +261,12 @@ static unsigned getRelocType32(MCContext &Ctx,
260261
case MCSymbolRefExpr::VK_TLSGD:
261262
assert(Type == RT32_32);
262263
assert(!IsPCRel);
263-
return ELF::R_386_TLS_GD;
264+
// LDC
265+
{
266+
auto ofi = Ctx.getObjectFileInfo();
267+
return ofi && ofi->getTargetTriple().isAndroid() ? ELF::R_386_GOT32
268+
: ELF::R_386_TLS_GD;
269+
}
264270
case MCSymbolRefExpr::VK_GOTTPOFF:
265271
assert(Type == RT32_32);
266272
assert(!IsPCRel);

0 commit comments

Comments
 (0)