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