Object File

Reference LLVM Source code

Parent -> Child: MCObjectFileInfo -> TargetLoweringObjectFile -> TargetLoweringObjectFileELF -> MipsTargetObjectFile

MCObjectFileInfo

SourceCode of class MCObjectFileInfo

  • llvm/MC/MCObjectFileInfo.h
  • llvm/lib/MC/MCObjectFileInfo.cpp

States:

  • MCSection *TextSection, *DataSection, *BSSSection
  • MCSection *ReadOnlySection. Not required.
  • MCSection *LSDASection. Section of Language Specific Data Area (LSDA). To support languages with exception handling.
  • DWARF sections
    • DwarfLineSection
    • DwarfLineStrSection
    • DwarfStrSection

Sections Initialization

Initialized in function MCObjectFileInfo::initELFMCObjectFileInfo(const Triple &T, bool Large).

code

DataSection

getDataSection() const { return DataSection; } getTLSExtraDataSection() const { return TLSExtraDataSection; } getTLSDataSection() const { return TLSDataSection; } getConstDataSection() const { return ConstDataSection; }

Called in:

  • MipsTargetStreamer.cpp: MipsTargetELFStreamer::finish()
    • from code: .bss, .text, .data are always at least 16-byte aligned.

TextSection

TargetLoweringObjectFileELF

This class handles lowerings for common ELF object file formats

Source code:

SelectSectionForGlobal

It will return a section for the input global address.

MCSection *TargetLoweringObjectFileELF::SelectSectionForGlobal

It will use getDataSections() to check whether different variables should use separate sections.

TargetMachine.h: the -fdata-sections and -ffunction-sections option: put every variable or function into different section:

//llvm/include/llvm/Target/TargetMachine.h

  /// Return true if data objects should be emitted into their own section,
  /// corresponds to -fdata-sections.
  bool getDataSections() const {
    return Options.DataSections;
  }

  /// Return true if functions should be emitted into their own section,
  /// corresponding to -ffunction-sections.
  bool getFunctionSections() const {
    return Options.FunctionSections;
  }

MipsTargetObjectFile

Source: llvm/lib/Target/Mips/MipsTargetObjectFile.h/cpp

Parent class: TargetLoweringObjectFileELF

Rules found in the source

About Small Data/BSS Sections:

  • Here only define target specific operations. For most target independent object file generation, use functions in parent class TargetLoweringObjectFileELF
  • An address must be loaded from a small section if its size is less than the small section size threshold.
  • Data in the small section must be addressed using gp_rel operator. Data can be
    • Global variables (SelectSectionForGlobal())
    • Constant data (SelectSectionForConstant())
  • To determine a section for global, will treat global declaration and definition differently. Because some place requires a global definitions instead of global declaration. See MipsTargetObjectFile::IsGlobalInSmallSection()
  • If IsGlobalInSmallSectionImpl() and global Kind.isData() || Kind.isBSS() || Kind.isCommon() || Kind.isReadOnly(), then the global should be placed into small data/bss section.
  • Do not put global variable into small sections, in the following cases:
    • If -mlocal-sdata is given, and global variable ->hasLocalLinkage()
    • If -extern-sdata is given, and global variable (GVA->hasExternalLinkage() && GVA->isDeclaration() ) || GVA-> hasCommonLinkage()
    • If -membedded-data is given, and global variable GVA->isConstant().

Initialize

(No constructor method.)

MipsTargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &TM)

  • Input:
    • MCContext &Ctx
    • const TargetMachine &TM
  • Output (elements being initialized)
    • SmallDataSection: .sdata in ELF. Init via getContext().getELFSection()
    • SmallBSSSection: .sbss in ELF. Init via getContext().getELFSection()
    • this->TM = &static_cast<const MipsTargetMachine &> (TM)

LLVM Basics learned in Code

  • To distinguish a global variable from functions in a GlobalObject

    const GlobalObject GO; // passed in as parameter.
    const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GO);
    if (!GVA)
    return false;
    
    // source: llvm/lib/Target/Mips/MipsTargetObjectFile.cpp: `IsGlobalInSmallSectionImpl()`
    
  • To reconginze the subtarget of the Mips Arch: use getSubtargetImpl() from TargetMacine class.

    TailPaddingAmount
    MipsTargetObjectFile::getTailPaddingForPreciseBounds(uint64_t Size) const {
    const MipsSubtarget &Subtarget =
      *static_cast<const MipsTargetMachine &>(*TM).getSubtargetImpl();
    if (!Subtarget.isCheri())
    return TailPaddingAmount::None;
    if (Subtarget.isCheri128()) {
    return static_cast<TailPaddingAmount>(
        llvm::alignTo(Size, cc128_get_required_alignment(Size)) - Size);
    }
    assert(Subtarget.isCheri256());
    // No padding required for CHERI256
    return TailPaddingAmount::None;
    }
    
    // source: llvm/lib/Target/Mips/MipsTargetObjectFile.cpp
    

Created Jul 17, 2020 // Last Updated Jul 19, 2020

If you could revise
the fundmental principles of
computer system design
to improve security...

... what would you change?