1f785676fSDimitry Andric //===-- PPCTargetObjectFile.cpp - PPC Object Info -------------------------===//
2f785676fSDimitry Andric //
3f785676fSDimitry Andric //                     The LLVM Compiler Infrastructure
4f785676fSDimitry Andric //
5f785676fSDimitry Andric // This file is distributed under the University of Illinois Open Source
6f785676fSDimitry Andric // License. See LICENSE.TXT for details.
7f785676fSDimitry Andric //
8f785676fSDimitry Andric //===----------------------------------------------------------------------===//
9f785676fSDimitry Andric 
10f785676fSDimitry Andric #include "PPCTargetObjectFile.h"
1191bc56edSDimitry Andric #include "llvm/IR/Mangler.h"
12f785676fSDimitry Andric #include "llvm/MC/MCContext.h"
13f785676fSDimitry Andric #include "llvm/MC/MCExpr.h"
14f785676fSDimitry Andric #include "llvm/MC/MCSectionELF.h"
15f785676fSDimitry Andric 
16f785676fSDimitry Andric using namespace llvm;
17f785676fSDimitry Andric 
18f785676fSDimitry Andric void
19f785676fSDimitry Andric PPC64LinuxTargetObjectFile::
Initialize(MCContext & Ctx,const TargetMachine & TM)20f785676fSDimitry Andric Initialize(MCContext &Ctx, const TargetMachine &TM) {
21f785676fSDimitry Andric   TargetLoweringObjectFileELF::Initialize(Ctx, TM);
22f785676fSDimitry Andric   InitializeELF(TM.Options.UseInitArray);
23f785676fSDimitry Andric }
24f785676fSDimitry Andric 
SelectSectionForGlobal(const GlobalObject * GO,SectionKind Kind,const TargetMachine & TM) const25ff0cc061SDimitry Andric MCSection *PPC64LinuxTargetObjectFile::SelectSectionForGlobal(
26*d88c1a5aSDimitry Andric     const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
27f785676fSDimitry Andric   // Here override ReadOnlySection to DataRelROSection for PPC64 SVR4 ABI
28f785676fSDimitry Andric   // when we have a constant that contains global relocations.  This is
29f785676fSDimitry Andric   // necessary because of this ABI's handling of pointers to functions in
30f785676fSDimitry Andric   // a shared library.  The address of a function is actually the address
31f785676fSDimitry Andric   // of a function descriptor, which resides in the .opd section.  Generated
32f785676fSDimitry Andric   // code uses the descriptor directly rather than going via the GOT as some
33f785676fSDimitry Andric   // other ABIs do, which means that initialized function pointers must
34f785676fSDimitry Andric   // reference the descriptor.  The linker must convert copy relocs of
35f785676fSDimitry Andric   // pointers to functions in shared libraries into dynamic relocations,
36f785676fSDimitry Andric   // because of an ordering problem with initialization of copy relocs and
37f785676fSDimitry Andric   // PLT entries.  The dynamic relocation will be initialized by the dynamic
38f785676fSDimitry Andric   // linker, so we must use DataRelROSection instead of ReadOnlySection.
39f785676fSDimitry Andric   // For more information, see the description of ELIMINATE_COPY_RELOCS in
40f785676fSDimitry Andric   // GNU ld.
4191bc56edSDimitry Andric   if (Kind.isReadOnly()) {
42*d88c1a5aSDimitry Andric     const auto *GVar = dyn_cast<GlobalVariable>(GO);
43f785676fSDimitry Andric 
447d523365SDimitry Andric     if (GVar && GVar->isConstant() && GVar->getInitializer()->needsRelocation())
4591bc56edSDimitry Andric       Kind = SectionKind::getReadOnlyWithRel();
4691bc56edSDimitry Andric   }
47f785676fSDimitry Andric 
48*d88c1a5aSDimitry Andric   return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind, TM);
49f785676fSDimitry Andric }
50f785676fSDimitry Andric 
51f785676fSDimitry Andric const MCExpr *PPC64LinuxTargetObjectFile::
getDebugThreadLocalSymbol(const MCSymbol * Sym) const52f785676fSDimitry Andric getDebugThreadLocalSymbol(const MCSymbol *Sym) const {
53f785676fSDimitry Andric   const MCExpr *Expr =
543ca95b02SDimitry Andric     MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_DTPREL, getContext());
5597bc6c73SDimitry Andric   return MCBinaryExpr::createAdd(Expr,
5697bc6c73SDimitry Andric                                  MCConstantExpr::create(0x8000, getContext()),
57f785676fSDimitry Andric                                  getContext());
58f785676fSDimitry Andric }
59f785676fSDimitry Andric 
60