10b57cec5SDimitry Andric //===-- PPCTargetObjectFile.cpp - PPC Object Info -------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric
90b57cec5SDimitry Andric #include "PPCTargetObjectFile.h"
105ffd83dbSDimitry Andric #include "llvm/IR/GlobalVariable.h"
110b57cec5SDimitry Andric #include "llvm/IR/Mangler.h"
120b57cec5SDimitry Andric #include "llvm/MC/MCContext.h"
130b57cec5SDimitry Andric #include "llvm/MC/MCExpr.h"
140b57cec5SDimitry Andric #include "llvm/MC/MCSectionELF.h"
150b57cec5SDimitry Andric
160b57cec5SDimitry Andric using namespace llvm;
170b57cec5SDimitry Andric
180b57cec5SDimitry Andric void
190b57cec5SDimitry Andric PPC64LinuxTargetObjectFile::
Initialize(MCContext & Ctx,const TargetMachine & TM)200b57cec5SDimitry Andric Initialize(MCContext &Ctx, const TargetMachine &TM) {
210b57cec5SDimitry Andric TargetLoweringObjectFileELF::Initialize(Ctx, TM);
220b57cec5SDimitry Andric }
230b57cec5SDimitry Andric
SelectSectionForGlobal(const GlobalObject * GO,SectionKind Kind,const TargetMachine & TM) const240b57cec5SDimitry Andric MCSection *PPC64LinuxTargetObjectFile::SelectSectionForGlobal(
250b57cec5SDimitry Andric const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
260b57cec5SDimitry Andric // Here override ReadOnlySection to DataRelROSection for PPC64 SVR4 ABI
270b57cec5SDimitry Andric // when we have a constant that contains global relocations. This is
280b57cec5SDimitry Andric // necessary because of this ABI's handling of pointers to functions in
290b57cec5SDimitry Andric // a shared library. The address of a function is actually the address
300b57cec5SDimitry Andric // of a function descriptor, which resides in the .opd section. Generated
310b57cec5SDimitry Andric // code uses the descriptor directly rather than going via the GOT as some
320b57cec5SDimitry Andric // other ABIs do, which means that initialized function pointers must
330b57cec5SDimitry Andric // reference the descriptor. The linker must convert copy relocs of
340b57cec5SDimitry Andric // pointers to functions in shared libraries into dynamic relocations,
350b57cec5SDimitry Andric // because of an ordering problem with initialization of copy relocs and
360b57cec5SDimitry Andric // PLT entries. The dynamic relocation will be initialized by the dynamic
370b57cec5SDimitry Andric // linker, so we must use DataRelROSection instead of ReadOnlySection.
380b57cec5SDimitry Andric // For more information, see the description of ELIMINATE_COPY_RELOCS in
390b57cec5SDimitry Andric // GNU ld.
400b57cec5SDimitry Andric if (Kind.isReadOnly()) {
410b57cec5SDimitry Andric const auto *GVar = dyn_cast<GlobalVariable>(GO);
420b57cec5SDimitry Andric
43*fe6060f1SDimitry Andric if (GVar && GVar->isConstant() &&
44*fe6060f1SDimitry Andric GVar->getInitializer()->needsDynamicRelocation())
450b57cec5SDimitry Andric Kind = SectionKind::getReadOnlyWithRel();
460b57cec5SDimitry Andric }
470b57cec5SDimitry Andric
480b57cec5SDimitry Andric return TargetLoweringObjectFileELF::SelectSectionForGlobal(GO, Kind, TM);
490b57cec5SDimitry Andric }
500b57cec5SDimitry Andric
510b57cec5SDimitry Andric const MCExpr *PPC64LinuxTargetObjectFile::
getDebugThreadLocalSymbol(const MCSymbol * Sym) const520b57cec5SDimitry Andric getDebugThreadLocalSymbol(const MCSymbol *Sym) const {
530b57cec5SDimitry Andric const MCExpr *Expr =
540b57cec5SDimitry Andric MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_DTPREL, getContext());
550b57cec5SDimitry Andric return MCBinaryExpr::createAdd(Expr,
560b57cec5SDimitry Andric MCConstantExpr::create(0x8000, getContext()),
570b57cec5SDimitry Andric getContext());
580b57cec5SDimitry Andric }
590b57cec5SDimitry Andric
60