xref: /llvm-project/llvm/lib/Target/PowerPC/PPCTargetObjectFile.cpp (revision f71cb9dbb739bb58ce7e52e49fe384ff2ff11687)
1 //===-- PPCTargetObjectFile.cpp - PPC Object Info -------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "PPCTargetObjectFile.h"
10 #include "llvm/IR/GlobalVariable.h"
11 #include "llvm/MC/MCContext.h"
12 #include "llvm/MC/MCExpr.h"
13 
14 using namespace llvm;
15 
16 void
17 PPC64LinuxTargetObjectFile::
18 Initialize(MCContext &Ctx, const TargetMachine &TM) {
19   TargetLoweringObjectFileELF::Initialize(Ctx, TM);
20 }
21 
22 MCSection *PPC64LinuxTargetObjectFile::SelectSectionForGlobal(
23     const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
24   // Here override ReadOnlySection to DataRelROSection for PPC64 SVR4 ABI
25   // when we have a constant that contains global relocations.  This is
26   // necessary because of this ABI's handling of pointers to functions in
27   // a shared library.  The address of a function is actually the address
28   // of a function descriptor, which resides in the .opd section.  Generated
29   // code uses the descriptor directly rather than going via the GOT as some
30   // other ABIs do, which means that initialized function pointers must
31   // reference the descriptor.  The linker must convert copy relocs of
32   // pointers to functions in shared libraries into dynamic relocations,
33   // because of an ordering problem with initialization of copy relocs and
34   // PLT entries.  The dynamic relocation will be initialized by the dynamic
35   // linker, so we must use DataRelROSection instead of ReadOnlySection.
36   // For more information, see the description of ELIMINATE_COPY_RELOCS in
37   // GNU ld.
38   if (Kind.isReadOnly()) {
39     const auto *GVar = dyn_cast<GlobalVariable>(GO);
40 
41     if (GVar && GVar->isConstant() &&
42         GVar->getInitializer()->needsDynamicRelocation())
43       Kind = SectionKind::getReadOnlyWithRel();
44   }
45 
46   return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind, TM);
47 }
48 
49 const MCExpr *PPC64LinuxTargetObjectFile::
50 getDebugThreadLocalSymbol(const MCSymbol *Sym) const {
51   const MCExpr *Expr =
52     MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_DTPREL, getContext());
53   return MCBinaryExpr::createAdd(Expr,
54                                  MCConstantExpr::create(0x8000, getContext()),
55                                  getContext());
56 }
57 
58