From 4d857664141a219bd06ed3cf8fb1816eac4d74a7 Mon Sep 17 00:00:00 2001 From: pommicket Date: Tue, 8 Nov 2022 17:46:10 -0500 Subject: emit warning on position-independent object file --- src/linker.rs | 15 +++++++++++---- src/main.rs | 1 - test.c | 4 +++- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/linker.rs b/src/linker.rs index 26e306b..fdc1f5b 100644 --- a/src/linker.rs +++ b/src/linker.rs @@ -120,18 +120,21 @@ pub enum LinkWarning { /// unsupported relocation type RelUnsupported(u8), /// relocation is too large - RelOOB(String, u64), + RelOob(String, u64), /// multiple definitions of a symbol MultipleDefinitions(String), + /// looks like a position-independent file. + MaybePic(String), } impl fmt::Display for LinkWarning { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { use LinkWarning::*; match self { - RelOOB(source, offset) => write!(f, "relocation {source}+0x{offset:x} goes outside of its section (it will be ignored)."), + RelOob(source, offset) => write!(f, "relocation {source}+0x{offset:x} goes outside of its section (it will be ignored)."), RelUnsupported(x) => write!(f, "Unsupported relocation type {x} (relocation ignored)."), MultipleDefinitions(name) => write!(f, "Symbol {name} has multiple definitions. One of them will be chosen arbitrarily."), + MaybePic(name) => write!(f, "{name} looks like a position-independent object. Try recompiling with -fno-pic?"), } } } @@ -956,6 +959,10 @@ impl<'a> Linker<'a> { symbol: &elf::Symbol, ) -> ObjectResult<()> { let name = elf.symbol_name(symbol)?; + if name == "_GLOBAL_OFFSET_TABLE_" { + self.emit_warning(LinkWarning::MaybePic(self.source_name(source).into())); + } + let name_id = self.symbol_names.add(name); let size = symbol.size; @@ -1005,6 +1012,7 @@ impl<'a> Linker<'a> { use ObjectError::*; let source_id = SourceId(self.sources.len() as _); + self.sources.push(name.into()); let elf = elf::Reader32LE::new(reader)?; if elf.r#type() != elf::Type::Rel { @@ -1033,7 +1041,6 @@ impl<'a> Linker<'a> { } self.relocations.push(relocations); - self.sources.push(name.into()); self.source_data.push(elf.to_data()); Ok(()) @@ -1162,7 +1169,7 @@ impl<'a> Linker<'a> { /// Apply relocation to executable. fn apply_relocation(&self, exec: &mut LinkerOutput, map: &OffsetMap, rel: &Relocation) { let warn_oob = || { - self.emit_warning(LinkWarning::RelOOB( + self.emit_warning(LinkWarning::RelOob( self.source_name(rel.r#where.0).into(), rel.r#where.1, )); diff --git a/src/main.rs b/src/main.rs index f0a3725..585d241 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,5 @@ /* @TODO: -- generate a warning/error on position-independent object files - static libraries */ diff --git a/test.c b/test.c index 86453de..31dee49 100644 --- a/test.c +++ b/test.c @@ -2,6 +2,8 @@ #include +int f() { return 13; } + void entry(void) { - exit(42); + exit(f()); } -- cgit v1.2.3