1259698Sdim//===-- PPCTargetObjectFile.cpp - PPC Object Info -------------------------===//
2259698Sdim//
3259698Sdim//                     The LLVM Compiler Infrastructure
4259698Sdim//
5259698Sdim// This file is distributed under the University of Illinois Open Source
6259698Sdim// License. See LICENSE.TXT for details.
7259698Sdim//
8259698Sdim//===----------------------------------------------------------------------===//
9259698Sdim
10259698Sdim#include "PPCTargetObjectFile.h"
11259698Sdim#include "llvm/MC/MCContext.h"
12259698Sdim#include "llvm/MC/MCExpr.h"
13259698Sdim#include "llvm/MC/MCSectionELF.h"
14259698Sdim#include "llvm/Target/Mangler.h"
15259698Sdim
16259698Sdimusing namespace llvm;
17259698Sdim
18259698Sdimvoid
19259698SdimPPC64LinuxTargetObjectFile::
20259698SdimInitialize(MCContext &Ctx, const TargetMachine &TM) {
21259698Sdim  TargetLoweringObjectFileELF::Initialize(Ctx, TM);
22259698Sdim  InitializeELF(TM.Options.UseInitArray);
23259698Sdim}
24259698Sdim
25259698Sdimconst MCSection * PPC64LinuxTargetObjectFile::
26259698SdimSelectSectionForGlobal(const GlobalValue *GV, SectionKind Kind,
27259698Sdim                       Mangler *Mang, const TargetMachine &TM) const {
28259698Sdim
29259698Sdim  const MCSection *DefaultSection =
30259698Sdim    TargetLoweringObjectFileELF::SelectSectionForGlobal(GV, Kind, Mang, TM);
31259698Sdim
32259698Sdim  if (DefaultSection != ReadOnlySection)
33259698Sdim    return DefaultSection;
34259698Sdim
35259698Sdim  // Here override ReadOnlySection to DataRelROSection for PPC64 SVR4 ABI
36259698Sdim  // when we have a constant that contains global relocations.  This is
37259698Sdim  // necessary because of this ABI's handling of pointers to functions in
38259698Sdim  // a shared library.  The address of a function is actually the address
39259698Sdim  // of a function descriptor, which resides in the .opd section.  Generated
40259698Sdim  // code uses the descriptor directly rather than going via the GOT as some
41259698Sdim  // other ABIs do, which means that initialized function pointers must
42259698Sdim  // reference the descriptor.  The linker must convert copy relocs of
43259698Sdim  // pointers to functions in shared libraries into dynamic relocations,
44259698Sdim  // because of an ordering problem with initialization of copy relocs and
45259698Sdim  // PLT entries.  The dynamic relocation will be initialized by the dynamic
46259698Sdim  // linker, so we must use DataRelROSection instead of ReadOnlySection.
47259698Sdim  // For more information, see the description of ELIMINATE_COPY_RELOCS in
48259698Sdim  // GNU ld.
49259698Sdim  const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
50259698Sdim
51259698Sdim  if (GVar && GVar->isConstant() &&
52259698Sdim      (GVar->getInitializer()->getRelocationInfo() ==
53259698Sdim       Constant::GlobalRelocations))
54259698Sdim    return DataRelROSection;
55259698Sdim
56259698Sdim  return DefaultSection;
57259698Sdim}
58259698Sdim
59259698Sdimconst MCExpr *PPC64LinuxTargetObjectFile::
60259698SdimgetDebugThreadLocalSymbol(const MCSymbol *Sym) const {
61259698Sdim  const MCExpr *Expr =
62259698Sdim    MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_PPC_DTPREL, getContext());
63259698Sdim  return MCBinaryExpr::CreateAdd(Expr,
64259698Sdim                                 MCConstantExpr::Create(0x8000, getContext()),
65259698Sdim                                 getContext());
66259698Sdim}
67259698Sdim
68