122d40dcfSBill Schmidt //===-- PPCTargetObjectFile.cpp - PPC Object Info -------------------------===//
222d40dcfSBill Schmidt //
322d40dcfSBill Schmidt //                     The LLVM Compiler Infrastructure
422d40dcfSBill Schmidt //
522d40dcfSBill Schmidt // This file is distributed under the University of Illinois Open Source
622d40dcfSBill Schmidt // License. See LICENSE.TXT for details.
722d40dcfSBill Schmidt //
822d40dcfSBill Schmidt //===----------------------------------------------------------------------===//
922d40dcfSBill Schmidt 
1022d40dcfSBill Schmidt #include "PPCTargetObjectFile.h"
11894843cbSRafael Espindola #include "llvm/IR/Mangler.h"
1222d40dcfSBill Schmidt #include "llvm/MC/MCContext.h"
1322d40dcfSBill Schmidt #include "llvm/MC/MCExpr.h"
1422d40dcfSBill Schmidt #include "llvm/MC/MCSectionELF.h"
1522d40dcfSBill Schmidt 
1622d40dcfSBill Schmidt using namespace llvm;
1722d40dcfSBill Schmidt 
1822d40dcfSBill Schmidt void
1922d40dcfSBill Schmidt PPC64LinuxTargetObjectFile::
2022d40dcfSBill Schmidt Initialize(MCContext &Ctx, const TargetMachine &TM) {
2122d40dcfSBill Schmidt   TargetLoweringObjectFileELF::Initialize(Ctx, TM);
2222d40dcfSBill Schmidt   InitializeELF(TM.Options.UseInitArray);
2322d40dcfSBill Schmidt }
2422d40dcfSBill Schmidt 
250709a7bdSRafael Espindola MCSection *PPC64LinuxTargetObjectFile::SelectSectionForGlobal(
26fa0f7283SRafael Espindola     const GlobalValue *GV, SectionKind Kind, Mangler &Mang,
27fa0f7283SRafael Espindola     const TargetMachine &TM) const {
286cda22a3SBill Schmidt   // Here override ReadOnlySection to DataRelROSection for PPC64 SVR4 ABI
2922d40dcfSBill Schmidt   // when we have a constant that contains global relocations.  This is
3022d40dcfSBill Schmidt   // necessary because of this ABI's handling of pointers to functions in
3122d40dcfSBill Schmidt   // a shared library.  The address of a function is actually the address
3222d40dcfSBill Schmidt   // of a function descriptor, which resides in the .opd section.  Generated
3322d40dcfSBill Schmidt   // code uses the descriptor directly rather than going via the GOT as some
3422d40dcfSBill Schmidt   // other ABIs do, which means that initialized function pointers must
3522d40dcfSBill Schmidt   // reference the descriptor.  The linker must convert copy relocs of
3622d40dcfSBill Schmidt   // pointers to functions in shared libraries into dynamic relocations,
3722d40dcfSBill Schmidt   // because of an ordering problem with initialization of copy relocs and
3822d40dcfSBill Schmidt   // PLT entries.  The dynamic relocation will be initialized by the dynamic
396cda22a3SBill Schmidt   // linker, so we must use DataRelROSection instead of ReadOnlySection.
4022d40dcfSBill Schmidt   // For more information, see the description of ELIMINATE_COPY_RELOCS in
4122d40dcfSBill Schmidt   // GNU ld.
42f4453998SUlrich Weigand   if (Kind.isReadOnly()) {
4322d40dcfSBill Schmidt     const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
4422d40dcfSBill Schmidt 
45*65e49021SRafael Espindola     if (GVar && GVar->isConstant() && GVar->getInitializer()->needsRelocation())
46f4453998SUlrich Weigand       Kind = SectionKind::getReadOnlyWithRel();
47f4453998SUlrich Weigand   }
4822d40dcfSBill Schmidt 
49f4453998SUlrich Weigand   return TargetLoweringObjectFileELF::SelectSectionForGlobal(GV, Kind,
50f4453998SUlrich Weigand                                                              Mang, TM);
5122d40dcfSBill Schmidt }
520f039824SUlrich Weigand 
530f039824SUlrich Weigand const MCExpr *PPC64LinuxTargetObjectFile::
540f039824SUlrich Weigand getDebugThreadLocalSymbol(const MCSymbol *Sym) const {
550f039824SUlrich Weigand   const MCExpr *Expr =
5613760bd1SJim Grosbach     MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_PPC_DTPREL, getContext());
5713760bd1SJim Grosbach   return MCBinaryExpr::createAdd(Expr,
5813760bd1SJim Grosbach                                  MCConstantExpr::create(0x8000, getContext()),
590f039824SUlrich Weigand                                  getContext());
600f039824SUlrich Weigand }
610f039824SUlrich Weigand 
62