Accept an address as the -e argument

`-e 0x401000` was looked up only as a symbol name, so the link warned
that the entry symbol was undefined and set e_entry to 0. GNU ld and lld
fall back to parsing the argument as a number (decimal, 0x hex or octal)
when no symbol has that name, and so does mold now.
This commit is contained in:
Rui Ueyama committed 2026-10-01 09:57:41 +09:00
1 parent b7048f555a
commit aed5a38d1d
3 files changed
+12 -1

No files matched your search

+7
View File
@@ -256,6 +256,13 @@ fn entry_addr<E: Target>(ctx: &Context<E>) -> u64 {
if let Some(FileId::Obj(_)) = sym.file() {
return sym.addr(ctx);
}
// As in GNU ld and lld, the entry point can also be given as an address
// if no symbol has that name.
let addr = std::str::from_utf8(sym.name()).ok().and_then(crate::cmdline::parse_c_number);
if let Some(addr) = addr {
return addr;
}
if !ctx.args.shared {
warn!("entry symbol is not defined: {sym}");
}
+1 -1
View File
@@ -845,7 +845,7 @@ fn parse_hex(opt: &str, value: &str) -> u64 {
}
/// Parses an integer in C syntax (decimal, `0x` hex or leading-zero octal).
fn parse_c_number(s: &str) -> Option<u64> {
pub(crate) fn parse_c_number(s: &str) -> Option<u64> {
let s = s.trim_start_matches(|c: char| c.is_ascii() && is_space(c as u8));
let (negative, digits) = if let Some(rest) = s.strip_prefix('-') {
(true, rest)
+4
View File
@@ -22,3 +22,7 @@ grep "Entry point address:.*0x1000$" $t/log
$CC -B. -o $t/exe2 -Wl,-e,bar $t/a.o $t/b.o
readelf -e $t/exe2 > $t/log
grep "Entry point address:.*0x2000$" $t/log
$CC -B. -o $t/exe3 -Wl,-e,0x3000 $t/a.o $t/b.o
readelf -e $t/exe3 > $t/log
grep "Entry point address:.*0x3000$" $t/log