mirror of
				https://github.com/c64scene-ar/llvm-6502.git
				synced 2025-11-04 05:17:07 +00:00 
			
		
		
		
	on 64-bit PowerPC ELF. The patch includes code to handle external assembly and MC output with the integrated assembler. It intentionally does not support the "old" JIT. For the initial-exec TLS model, the ABI requires the following to calculate the address of external thread-local variable x: Code sequence Relocation Symbol ld 9,x@got@tprel(2) R_PPC64_GOT_TPREL16_DS x add 9,9,x@tls R_PPC64_TLS x The register 9 is arbitrary here. The linker will replace x@got@tprel with the offset relative to the thread pointer to the generated GOT entry for symbol x. It will replace x@tls with the thread-pointer register (13). The two test cases verify correct assembly output and relocation output as just described. PowerPC-specific selection node variants are added for the two instructions above: LD_GOT_TPREL and ADD_TLS. These are inserted when an initial-exec global variable is encountered by PPCTargetLowering::LowerGlobalTLSAddress(), and later lowered to machine instructions LDgotTPREL and ADD8TLS. LDgotTPREL is a pseudo that uses the same LDrs support added for medium code model's LDtocL, with a different relocation type. The rest of the processing is straightforward. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@169281 91177308-0d34-0410-b5e6-96231b3b80d8
		
			
				
	
	
		
			200 lines
		
	
	
		
			6.5 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			200 lines
		
	
	
		
			6.5 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
//===-- PPCELFObjectWriter.cpp - PPC ELF Writer ---------------------------===//
 | 
						|
//
 | 
						|
//                     The LLVM Compiler Infrastructure
 | 
						|
//
 | 
						|
// This file is distributed under the University of Illinois Open Source
 | 
						|
// License. See LICENSE.TXT for details.
 | 
						|
//
 | 
						|
//===----------------------------------------------------------------------===//
 | 
						|
 | 
						|
#include "MCTargetDesc/PPCMCTargetDesc.h"
 | 
						|
#include "MCTargetDesc/PPCFixupKinds.h"
 | 
						|
#include "llvm/MC/MCELFObjectWriter.h"
 | 
						|
#include "llvm/MC/MCExpr.h"
 | 
						|
#include "llvm/MC/MCValue.h"
 | 
						|
#include "llvm/Support/ErrorHandling.h"
 | 
						|
 | 
						|
using namespace llvm;
 | 
						|
 | 
						|
namespace {
 | 
						|
  class PPCELFObjectWriter : public MCELFObjectTargetWriter {
 | 
						|
  public:
 | 
						|
    PPCELFObjectWriter(bool Is64Bit, uint8_t OSABI);
 | 
						|
 | 
						|
    virtual ~PPCELFObjectWriter();
 | 
						|
  protected:
 | 
						|
    virtual unsigned getRelocTypeInner(const MCValue &Target,
 | 
						|
                                       const MCFixup &Fixup,
 | 
						|
                                       bool IsPCRel) const;
 | 
						|
    virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
 | 
						|
                                  bool IsPCRel, bool IsRelocWithSymbol,
 | 
						|
                                  int64_t Addend) const;
 | 
						|
    virtual const MCSymbol *undefinedExplicitRelSym(const MCValue &Target,
 | 
						|
                                                    const MCFixup &Fixup,
 | 
						|
                                                    bool IsPCRel) const;
 | 
						|
    virtual void adjustFixupOffset(const MCFixup &Fixup, uint64_t &RelocOffset);
 | 
						|
  };
 | 
						|
}
 | 
						|
 | 
						|
PPCELFObjectWriter::PPCELFObjectWriter(bool Is64Bit, uint8_t OSABI)
 | 
						|
  : MCELFObjectTargetWriter(Is64Bit, OSABI,
 | 
						|
                            Is64Bit ?  ELF::EM_PPC64 : ELF::EM_PPC,
 | 
						|
                            /*HasRelocationAddend*/ true) {}
 | 
						|
 | 
						|
PPCELFObjectWriter::~PPCELFObjectWriter() {
 | 
						|
}
 | 
						|
 | 
						|
unsigned PPCELFObjectWriter::getRelocTypeInner(const MCValue &Target,
 | 
						|
                                               const MCFixup &Fixup,
 | 
						|
                                               bool IsPCRel) const
 | 
						|
{
 | 
						|
  MCSymbolRefExpr::VariantKind Modifier = Target.isAbsolute() ?
 | 
						|
    MCSymbolRefExpr::VK_None : Target.getSymA()->getKind();
 | 
						|
 | 
						|
  // determine the type of the relocation
 | 
						|
  unsigned Type;
 | 
						|
  if (IsPCRel) {
 | 
						|
    switch ((unsigned)Fixup.getKind()) {
 | 
						|
    default:
 | 
						|
      llvm_unreachable("Unimplemented");
 | 
						|
    case PPC::fixup_ppc_br24:
 | 
						|
      Type = ELF::R_PPC_REL24;
 | 
						|
      break;
 | 
						|
    case FK_PCRel_4:
 | 
						|
      Type = ELF::R_PPC_REL32;
 | 
						|
      break;
 | 
						|
    }
 | 
						|
  } else {
 | 
						|
    switch ((unsigned)Fixup.getKind()) {
 | 
						|
      default: llvm_unreachable("invalid fixup kind!");
 | 
						|
    case PPC::fixup_ppc_br24:
 | 
						|
      Type = ELF::R_PPC_ADDR24;
 | 
						|
      break;
 | 
						|
    case PPC::fixup_ppc_brcond14:
 | 
						|
      Type = ELF::R_PPC_ADDR14; // XXX: or BRNTAKEN?_
 | 
						|
      break;
 | 
						|
    case PPC::fixup_ppc_ha16:
 | 
						|
      switch (Modifier) {
 | 
						|
      default: llvm_unreachable("Unsupported Modifier");
 | 
						|
      case MCSymbolRefExpr::VK_PPC_TPREL16_HA:
 | 
						|
        Type = ELF::R_PPC_TPREL16_HA;
 | 
						|
        break;
 | 
						|
      case MCSymbolRefExpr::VK_None:
 | 
						|
        Type = ELF::R_PPC_ADDR16_HA;
 | 
						|
	break;
 | 
						|
      case MCSymbolRefExpr::VK_PPC_TOC16_HA:
 | 
						|
        Type = ELF::R_PPC64_TOC16_HA;
 | 
						|
        break;
 | 
						|
      }
 | 
						|
      break;
 | 
						|
    case PPC::fixup_ppc_lo16:
 | 
						|
      switch (Modifier) {
 | 
						|
      default: llvm_unreachable("Unsupported Modifier");
 | 
						|
      case MCSymbolRefExpr::VK_PPC_TPREL16_LO:
 | 
						|
        Type = ELF::R_PPC_TPREL16_LO;
 | 
						|
        break;
 | 
						|
      case MCSymbolRefExpr::VK_None:
 | 
						|
        Type = ELF::R_PPC_ADDR16_LO;
 | 
						|
	break;
 | 
						|
      case MCSymbolRefExpr::VK_PPC_TOC16_LO:
 | 
						|
        Type = ELF::R_PPC64_TOC16_LO;
 | 
						|
        break;
 | 
						|
      }
 | 
						|
      break;
 | 
						|
    case PPC::fixup_ppc_lo14:
 | 
						|
      Type = ELF::R_PPC_ADDR14;
 | 
						|
      break;
 | 
						|
    case PPC::fixup_ppc_toc:
 | 
						|
      Type = ELF::R_PPC64_TOC;
 | 
						|
      break;
 | 
						|
    case PPC::fixup_ppc_toc16:
 | 
						|
      Type = ELF::R_PPC64_TOC16;
 | 
						|
      break;
 | 
						|
    case PPC::fixup_ppc_toc16_ds:
 | 
						|
      switch (Modifier) {
 | 
						|
      default: llvm_unreachable("Unsupported Modifier");
 | 
						|
      case MCSymbolRefExpr::VK_PPC_TOC_ENTRY:
 | 
						|
        Type = ELF::R_PPC64_TOC16_DS;
 | 
						|
	break;
 | 
						|
      case MCSymbolRefExpr::VK_PPC_TOC16_LO:
 | 
						|
        Type = ELF::R_PPC64_TOC16_LO_DS;
 | 
						|
        break;
 | 
						|
      case MCSymbolRefExpr::VK_PPC_GOT_TPREL16_DS:
 | 
						|
        Type = ELF::R_PPC64_GOT_TPREL16_DS;
 | 
						|
        break;
 | 
						|
      }
 | 
						|
      break;
 | 
						|
    case PPC::fixup_ppc_tlsreg:
 | 
						|
      Type = ELF::R_PPC64_TLS;
 | 
						|
      break;
 | 
						|
    case FK_Data_8:
 | 
						|
      switch (Modifier) {
 | 
						|
      default: llvm_unreachable("Unsupported Modifier");
 | 
						|
      case MCSymbolRefExpr::VK_PPC_TOC:
 | 
						|
        Type = ELF::R_PPC64_TOC;
 | 
						|
        break;
 | 
						|
      case MCSymbolRefExpr::VK_None:
 | 
						|
        Type = ELF::R_PPC64_ADDR64;
 | 
						|
	break;
 | 
						|
      }
 | 
						|
      break;
 | 
						|
    case FK_Data_4:
 | 
						|
      Type = ELF::R_PPC_ADDR32;
 | 
						|
      break;
 | 
						|
    case FK_Data_2:
 | 
						|
      Type = ELF::R_PPC_ADDR16;
 | 
						|
      break;
 | 
						|
    }
 | 
						|
  }
 | 
						|
  return Type;
 | 
						|
}
 | 
						|
 | 
						|
unsigned PPCELFObjectWriter::GetRelocType(const MCValue &Target,
 | 
						|
                                          const MCFixup &Fixup,
 | 
						|
                                          bool IsPCRel,
 | 
						|
                                          bool IsRelocWithSymbol,
 | 
						|
                                          int64_t Addend) const {
 | 
						|
  return getRelocTypeInner(Target, Fixup, IsPCRel);
 | 
						|
}
 | 
						|
 | 
						|
const MCSymbol *PPCELFObjectWriter::undefinedExplicitRelSym(const MCValue &Target,
 | 
						|
                                                            const MCFixup &Fixup,
 | 
						|
                                                            bool IsPCRel) const {
 | 
						|
  assert(Target.getSymA() && "SymA cannot be 0");
 | 
						|
  const MCSymbol &Symbol = Target.getSymA()->getSymbol().AliasedSymbol();
 | 
						|
 | 
						|
  unsigned RelocType = getRelocTypeInner(Target, Fixup, IsPCRel);
 | 
						|
 | 
						|
  // The .odp creation emits a relocation against the symbol ".TOC." which
 | 
						|
  // create a R_PPC64_TOC relocation. However the relocation symbol name
 | 
						|
  // in final object creation should be NULL, since the symbol does not
 | 
						|
  // really exist, it is just the reference to TOC base for the current
 | 
						|
  // object file.
 | 
						|
  bool EmitThisSym = RelocType != ELF::R_PPC64_TOC;
 | 
						|
 | 
						|
  if (EmitThisSym && !Symbol.isTemporary())
 | 
						|
    return &Symbol;
 | 
						|
  return NULL;
 | 
						|
}
 | 
						|
 | 
						|
void PPCELFObjectWriter::
 | 
						|
adjustFixupOffset(const MCFixup &Fixup, uint64_t &RelocOffset) {
 | 
						|
  switch ((unsigned)Fixup.getKind()) {
 | 
						|
    case PPC::fixup_ppc_ha16:
 | 
						|
    case PPC::fixup_ppc_lo16:
 | 
						|
    case PPC::fixup_ppc_toc16:
 | 
						|
    case PPC::fixup_ppc_toc16_ds:
 | 
						|
      RelocOffset += 2;
 | 
						|
      break;
 | 
						|
    default:
 | 
						|
      break;
 | 
						|
  }
 | 
						|
}
 | 
						|
 | 
						|
MCObjectWriter *llvm::createPPCELFObjectWriter(raw_ostream &OS,
 | 
						|
                                               bool Is64Bit,
 | 
						|
                                               uint8_t OSABI) {
 | 
						|
  MCELFObjectTargetWriter *MOTW = new PPCELFObjectWriter(Is64Bit, OSABI);
 | 
						|
  return createELFObjectWriter(MOTW, OS,  /*IsLittleEndian=*/false);
 | 
						|
}
 |