Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions libwild/src/elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ use linker_utils::elf::secnames;
use linker_utils::elf::secnames::*;
use linker_utils::elf::shf;
use linker_utils::elf::sht;
use linker_utils::elf::stt;
use linker_utils::relaxation::RelocationModifier;
use linker_utils::utils::read_string;
use linker_utils::utils::read_u32;
Expand Down Expand Up @@ -166,7 +165,7 @@ pub(crate) type NoteHeader = object::elf::NoteHeader64<LittleEndian>;
type SectionTable<'data> = object::read::elf::SectionTable<'data, FileHeader>;
type SymbolTable<'data> = object::read::elf::SymbolTable<'data, FileHeader>;

#[derive(Debug, Copy, Clone)]
#[derive(Debug, Copy, Clone, Default)]
pub(crate) struct Elf;

#[derive(derive_more::Debug)]
Expand Down Expand Up @@ -808,7 +807,7 @@ impl platform::Platform for Elf {
}

fn create_linker_defined_symbols(
symbols: &mut crate::parsing::InternalSymbolsBuilder,
symbols: &mut crate::parsing::InternalSymbolsBuilder<Elf>,
output_kind: OutputKind,
args: &ElfArgs,
) {
Expand Down Expand Up @@ -908,15 +907,16 @@ impl platform::Platform for Elf {
// We define _TLS_MODULE_BASE_ either at the start or end of the TLS segment, depending on
// whether we're building a shared object or an executable. This symbol is used for TLSDESC.
// See https://www.fsfla.org/~lxoliva/writeups/TLS/RFC-TLSDESC-x86.txt for more details.
let mut elf_symbol = SymtabEntry::default();
elf_symbol.set_st_info(object::elf::STB_GLOBAL, object::elf::STT_TLS);
symbols.add_symbol(InternalSymDefInfo {
placement: if output_kind == OutputKind::SharedObject {
SymbolPlacement::SectionStart(output_section_id::TDATA)
} else {
SymbolPlacement::SectionEnd(output_section_id::TBSS)
},
name: b"_TLS_MODULE_BASE_",
elf_symbol_type: stt::TLS,
is_hidden: false,
symbol: elf_symbol,
});

// When `-z pack-relative-relocs` is used, Glibc requires this special version to be
Expand Down Expand Up @@ -1498,12 +1498,12 @@ impl platform::Platform for Elf {

fn allocate_internal_symbol(
symbol_id: SymbolId,
def_info: &InternalSymDefInfo,
def_info: &InternalSymDefInfo<Elf>,
sizes: &mut OutputSectionPartMap<u64>,
symbol_db: &SymbolDb<Self>,
) -> Result {
// PROVIDE_HIDDEN symbols are local, others are global
let symtab_part = if def_info.is_hidden {
let symtab_part = if def_info.symbol.is_hidden() {
part_id::SYMTAB_LOCAL
} else {
part_id::SYMTAB_GLOBAL
Expand Down Expand Up @@ -1936,6 +1936,10 @@ impl platform::Platform for Elf {
) {
*mem_offset = segment_alignment.align_modulo(*file_offset as u64, *mem_offset);
}

fn default_symtab_entry() -> Self::SymtabEntry {
Default::default()
}
}

impl<'data> platform::ObjectFile<'data> for File<'data> {
Expand Down Expand Up @@ -2855,6 +2859,16 @@ impl platform::Symbol for SymtabEntry {
fn is_gnu_unique(&self) -> bool {
self.st_bind() == object::elf::STB_GNU_UNIQUE
}

fn with_hidden(mut self, hidden: bool) -> Self {
self.st_other &= !0x3;
self.st_other |= if hidden {
object::elf::STV_HIDDEN
} else {
object::elf::STV_DEFAULT
};
self
}
}

pub(crate) fn convert_elf_visibility(st_visibility: u8) -> Visibility {
Expand Down
30 changes: 14 additions & 16 deletions libwild/src/elf_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ use linker_utils::elf::secnames::DYNSYM_SECTION_NAME_STR;
use linker_utils::elf::secnames::NOTE_GNU_BUILD_ID_SECTION_NAME_STR;
use linker_utils::elf::shf;
use linker_utils::elf::sht;
use linker_utils::elf::stt;
use linker_utils::loongarch64::highest_relocation_with_bias;
use linker_utils::relaxation::RelocationModifier;
use linker_utils::relaxation::SectionRelaxDeltas;
Expand Down Expand Up @@ -2012,9 +2011,8 @@ fn write_symbols<'data>(
}

if layout.args().should_output_partial_object() {
let e = LittleEndian;
for (sym_index, sym) in object.object.symbols.enumerate() {
if !sym.is_undefined(e) {
if !platform::Symbol::is_undefined(sym) {
continue;
}
let Ok(name) = object.object.symbol_name(sym) else {
Expand Down Expand Up @@ -3592,7 +3590,7 @@ pub(crate) struct EpilogueOffsets {
}

fn write_linker_script_state<'data, A: Arch<Platform = Elf>>(
script: &LinkerScriptLayoutState,
script: &LinkerScriptLayoutState<Elf>,
table_writer: &mut TableWriter,
layout: &ElfLayout<'data>,
) -> Result {
Expand All @@ -3610,7 +3608,7 @@ fn write_linker_script_state<'data, A: Arch<Platform = Elf>>(
}

fn write_synthetic_symbols<'data, A: Arch<Platform = Elf>>(
syn: &SyntheticSymbolsLayout,
syn: &SyntheticSymbolsLayout<Elf>,
table_writer: &mut TableWriter,
layout: &ElfLayout<'data>,
) -> Result {
Expand Down Expand Up @@ -3993,7 +3991,7 @@ fn write_linker_script_dynsym(
dynsym_writer: &mut SymbolTableWriter,
layout: &ElfLayout,
symbol_id: SymbolId,
script: &LinkerScriptLayoutState,
script: &LinkerScriptLayoutState<Elf>,
) -> Result {
let local_index = script
.internal_symbols
Expand Down Expand Up @@ -4074,7 +4072,7 @@ fn get_symbol_attributes(layout: &ElfLayout, symbol_id: SymbolId) -> Result<(u32
.output_index_of_section(section_id)
.unwrap_or(u32::from(object::elf::SHN_ABS))
});
Ok((shndx, def_info.elf_symbol_type.raw()))
Ok((shndx, def_info.symbol.st_type()))
}
crate::grouping::SequencedInput::SyntheticSymbols(_) => {
// For other non-object files (e.g. epilogue), default to ABS
Expand Down Expand Up @@ -4106,7 +4104,7 @@ fn write_internal_dynsym(
dynsym_writer: &mut SymbolTableWriter,
layout: &ElfLayout,
symbol_id: SymbolId,
def_info: &crate::parsing::InternalSymDefInfo,
def_info: &crate::parsing::InternalSymDefInfo<Elf>,
) -> Result {
if matches!(
def_info.placement,
Expand Down Expand Up @@ -4145,7 +4143,7 @@ fn write_defsym_dynsym(
dynsym_writer: &mut SymbolTableWriter,
layout: &ElfLayout,
symbol_id: SymbolId,
def_info: &crate::parsing::InternalSymDefInfo,
def_info: &crate::parsing::InternalSymDefInfo<Elf>,
) -> Result {
debug_assert!(matches!(
def_info.placement,
Expand Down Expand Up @@ -4303,7 +4301,7 @@ fn write_regular_object_dynamic_symbol_definition<'data>(
format!("Failed to copy dynamic {}", layout.symbol_debug(symbol_id))
})?;
}
} else if sym.is_common(LittleEndian) {
} else if platform::Symbol::is_common(sym) {
let symbol_id = sym_def.symbol_id;
let resolution = layout.local_symbol_resolution(symbol_id).with_context(|| {
format!(
Expand All @@ -4330,7 +4328,7 @@ fn write_regular_object_dynamic_symbol_definition<'data>(
.with_context(|| {
format!("Failed to copy dynamic {}", layout.symbol_debug(symbol_id))
})?;
} else if sym.is_absolute(LittleEndian) {
} else if platform::Symbol::is_absolute(sym) {
dynamic_symbol_writer
.copy_absolute_symbol(sym, name, ValueFlags::empty())
.with_context(|| {
Expand All @@ -4353,7 +4351,7 @@ fn write_regular_object_dynamic_symbol_definition<'data>(
}

fn write_internal_symbols(
internal_symbols: &InternalSymbols,
internal_symbols: &InternalSymbols<Elf>,
layout: &ElfLayout,
symbol_writer: &mut SymbolTableWriter<'_, '_>,
) -> Result {
Expand Down Expand Up @@ -4408,7 +4406,7 @@ fn write_internal_symbols(
.transpose()?
.unwrap_or(u32::from(object::elf::SHN_ABS));

(shndx, def_info.elf_symbol_type.raw())
(shndx, def_info.symbol.st_type())
};

// Move symbols that are in our header (section 0) into the first section, otherwise they'll
Expand All @@ -4419,7 +4417,7 @@ fn write_internal_symbols(

let mut address = resolution.value();

if def_info.elf_symbol_type == stt::TLS {
if platform::Symbol::is_tls(&def_info.symbol) {
address -= layout.tls_start_address();
}

Expand All @@ -4431,7 +4429,7 @@ fn write_internal_symbols(
}

// PROVIDE_HIDDEN symbols should be local, not global
let st_bind = if def_info.is_hidden {
let st_bind = if platform::Symbol::is_hidden(&def_info.symbol) {
object::elf::STB_LOCAL
} else {
object::elf::STB_GLOBAL
Expand Down Expand Up @@ -5061,7 +5059,7 @@ impl<'out> ProgramHeaderWriter<'out> {
}

fn write_internal_symbols_plt_got_entries<'data, A: Arch<Platform = Elf>>(
internal_symbols: &InternalSymbols,
internal_symbols: &InternalSymbols<Elf>,
table_writer: &mut TableWriter,
layout: &ElfLayout<'data>,
) -> Result {
Expand Down
18 changes: 9 additions & 9 deletions libwild/src/grouping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ use std::fmt::Display;

#[derive(Debug)]
pub(crate) enum Group<'data, P: Platform> {
Prelude(Prelude<'data>),
Prelude(Prelude<'data, P>),
Objects(&'data [SequencedInputObject<'data, P>]),
LinkerScripts(Vec<SequencedLinkerScript<'data>>),
LinkerScripts(Vec<SequencedLinkerScript<'data, P>>),
SyntheticSymbols(SyntheticSymbols),
#[cfg(feature = "plugins")]
LtoInputs(Vec<crate::linker_plugins::LtoInput<'data>>),
Expand All @@ -36,17 +36,17 @@ pub(crate) struct SequencedInputObject<'data, P: Platform> {
}

#[derive(Debug)]
pub(crate) struct SequencedLinkerScript<'data> {
pub(crate) parsed: ProcessedLinkerScript<'data>,
pub(crate) struct SequencedLinkerScript<'data, P: Platform> {
pub(crate) parsed: ProcessedLinkerScript<'data, P>,
pub(crate) symbol_id_range: SymbolIdRange,
pub(crate) file_id: FileId,
}

#[derive(Debug)]
pub(crate) enum SequencedInput<'db, 'data, P: Platform> {
Prelude(&'db Prelude<'data>),
Prelude(&'db Prelude<'data, P>),
Object(&'data SequencedInputObject<'data, P>),
LinkerScript(&'db SequencedLinkerScript<'data>),
LinkerScript(&'db SequencedLinkerScript<'data, P>),
SyntheticSymbols(&'db SyntheticSymbols),
#[cfg(feature = "plugins")]
LtoInput(&'db crate::linker_plugins::LtoInput<'data>),
Expand Down Expand Up @@ -103,7 +103,7 @@ impl<'data, P: Platform> Group<'data, P> {
pub(crate) fn create_groups<'data, P: Platform>(
symbol_db: &mut SymbolDb<'data, P>,
parsed_objects: Vec<Box<ParsedInputObject<'data, P>>>,
linker_scripts: Vec<ProcessedLinkerScript<'data>>,
linker_scripts: Vec<ProcessedLinkerScript<'data, P>>,
) {
timing_phase!("Group files");

Expand Down Expand Up @@ -159,7 +159,7 @@ pub(crate) fn create_groups<'data, P: Platform>(
}
}

let linker_scripts: Vec<SequencedLinkerScript<'_>> = linker_scripts
let linker_scripts: Vec<SequencedLinkerScript<'_, P>> = linker_scripts
.into_iter()
.enumerate()
.map(|(i, script)| {
Expand Down Expand Up @@ -278,7 +278,7 @@ impl<'data, P: Platform> SequencedInputObject<'data, P> {
}
}

impl<'data> SequencedLinkerScript<'data> {
impl<'data, P: Platform> SequencedLinkerScript<'data, P> {
pub(crate) fn symbol_name(&self, symbol_id: SymbolId) -> UnversionedSymbolName<'data> {
let local_index = self.symbol_id_range.id_to_offset(symbol_id);
UnversionedSymbolName::new(self.parsed.symbol_defs[local_index].name)
Expand Down
Loading
Loading