blob: a049dc3fda93b42b8ce0c1e416b82e04e9e9a737 [file] [log] [blame]
Bill Schmidt22d40dc2013-05-13 19:34:37 +00001//===-- PPCTargetObjectFile.cpp - PPC Object Info -------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "PPCTargetObjectFile.h"
Rafael Espindola894843c2014-01-07 21:19:40 +000011#include "llvm/IR/Mangler.h"
Bill Schmidt22d40dc2013-05-13 19:34:37 +000012#include "llvm/MC/MCContext.h"
13#include "llvm/MC/MCExpr.h"
14#include "llvm/MC/MCSectionELF.h"
Bill Schmidt22d40dc2013-05-13 19:34:37 +000015
16using namespace llvm;
17
18void
19PPC64LinuxTargetObjectFile::
20Initialize(MCContext &Ctx, const TargetMachine &TM) {
21 TargetLoweringObjectFileELF::Initialize(Ctx, TM);
22 InitializeELF(TM.Options.UseInitArray);
23}
24
Rafael Espindola0709a7b2015-05-21 19:20:38 +000025MCSection *PPC64LinuxTargetObjectFile::SelectSectionForGlobal(
Peter Collingbourne67335642016-10-24 19:23:39 +000026 const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
Bill Schmidt6cda22a2013-05-13 19:40:36 +000027 // Here override ReadOnlySection to DataRelROSection for PPC64 SVR4 ABI
Bill Schmidt22d40dc2013-05-13 19:34:37 +000028 // when we have a constant that contains global relocations. This is
29 // necessary because of this ABI's handling of pointers to functions in
30 // a shared library. The address of a function is actually the address
31 // of a function descriptor, which resides in the .opd section. Generated
32 // code uses the descriptor directly rather than going via the GOT as some
33 // other ABIs do, which means that initialized function pointers must
34 // reference the descriptor. The linker must convert copy relocs of
35 // pointers to functions in shared libraries into dynamic relocations,
36 // because of an ordering problem with initialization of copy relocs and
37 // PLT entries. The dynamic relocation will be initialized by the dynamic
Bill Schmidt6cda22a2013-05-13 19:40:36 +000038 // linker, so we must use DataRelROSection instead of ReadOnlySection.
Bill Schmidt22d40dc2013-05-13 19:34:37 +000039 // For more information, see the description of ELIMINATE_COPY_RELOCS in
40 // GNU ld.
Ulrich Weigandf4453992014-03-14 12:45:22 +000041 if (Kind.isReadOnly()) {
Peter Collingbourne67335642016-10-24 19:23:39 +000042 const auto *GVar = dyn_cast<GlobalVariable>(GO);
Bill Schmidt22d40dc2013-05-13 19:34:37 +000043
Rafael Espindola65e49022015-11-17 00:51:23 +000044 if (GVar && GVar->isConstant() && GVar->getInitializer()->needsRelocation())
Ulrich Weigandf4453992014-03-14 12:45:22 +000045 Kind = SectionKind::getReadOnlyWithRel();
46 }
Bill Schmidt22d40dc2013-05-13 19:34:37 +000047
Peter Collingbourne67335642016-10-24 19:23:39 +000048 return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind, TM);
Bill Schmidt22d40dc2013-05-13 19:34:37 +000049}
Ulrich Weigand0f039822013-07-02 18:47:35 +000050
51const MCExpr *PPC64LinuxTargetObjectFile::
52getDebugThreadLocalSymbol(const MCSymbol *Sym) const {
53 const MCExpr *Expr =
Colin LeMahieu0e051922016-02-10 18:32:01 +000054 MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_DTPREL, getContext());
Jim Grosbach13760bd2015-05-30 01:25:56 +000055 return MCBinaryExpr::createAdd(Expr,
56 MCConstantExpr::create(0x8000, getContext()),
Ulrich Weigand0f039822013-07-02 18:47:35 +000057 getContext());
58}
59