Assembly, Link, and Load

Cards (42)

  • Relocatable Object File

    Object file created by the assembler
  • Given one assembly program (p1.s) the assembler will create one relocatable object file (p1.o)
  • Object file is short for relocatable object file
  • Bytes in the relocatable object file are in a very specific format: executable and linkable format (ELF)
  • Each machine instruction generated by the assembler is assigned to an ELF section and given a temporary memory address (if possible)
  • ELF Sections

    • .text section
    • .rodata section
    • .data section
    • .symtab section
    • .rel.text section
    • .rel.data section
    • ELF header
  • .text section

    Contains machine instructions (i.e., your program)
  • .rodata section
    Contains read only data (such as constants)
  • .data section
    Contains initialized global and static variables
  • .symtab section
    Contains the symbol table with name and address location of functions and global/static variables
  • .rel.text section
    Contains relocation info for .text section, linker will relocate unresolved instructions and addresses
  • .rel.data section
    Contains relocation info for .data section, linker will relocate unresolved data and addresses
  • ELF header
    Part of the ELF standard binary format
  • ELF is the standard binary format for all object files created by the compilation system
  • Types of object files
    • Relocatable object file (.o)
    • Executable object file (a.out)
    • Shared object file (.so)
  • Assembler Operations
    1. Identify symbols and update .symtab section
    2. Translate assembly data to machine data and update .data section
    3. Translate assembly instructions to machine instructions and update .text section
    4. Backfill address information in .symtab and .text sections (if possible)
    5. Update .rel.text section with entries for unresolved symbols in .text section
  • Assembler first identifies all the symbols in the assembly program and updates the .symtab section
  • A symbol may be the name of a function, global variable, or static variable
  • Symbol table entry includes symbol name, section, and memory address of instruction or data
  • Unknown address information (?) must be resolved by the assembler or the linker
  • Assembler translates assembly data to machine data and updates the .data section
  • Each variable is assigned an address, and the number of bytes depends on the data-type, data structure, etc.
  • Total number of bytes in the .data section is fixed
  • Assembler translates assembly instructions to machine instructions and updates the .text section
  • Each entry is a 32-bit (4 byte) machine instruction that is assigned an address
  • Total number of bytes in the .text section is fixed (i.e., number of instructions x 4 bytes)
  • If possible, assembler updates address information in the .symtab section
  • If not possible, address is unresolved (?), and linker must relocate
  • Assembler updates the .rel.text section and adds an entry for each unresolved symbol in the .text section
  • The .rel.text section is used by the linker to efficiently update the .text section when relocation is performed
  • Linker Step
    1. Symbol resolution
    2. Relocation
  • Symbol resolution
    For each symbol in the symbol table that has an unknown address, the linker attempts to locate the same symbol (with the same name and section type) in other symbol tables
  • If symbol resolution is not possible, linking step stops and fails with undefined reference error
  • Relocation
    After symbol resolution is performed, the linker relocates (copies) the .text and .data sections from one or more relocatable object files to the .text section and .data section in an executable object file
  • Relocation copies data and instructions from their relative address locations in the relocatable object file to their final absolute address locations in the executable object file
  • Relocation ensures all data and instructions copied to the executable object file are given a memory address, and the executable is "fully linked"
  • The linker updates the .text section in the executable object file with the resolved addresses from the .rel.text section
  • Loading
    Copying the program sections from the executable object file into main memory
  • The operating system assigns memory for the stack, heap, text, and data segments when the program is executed
  • The loader copies the fully linked ELF sections to the appropriate main memory address locations