use xold::dynamic::ZOptions;
const SATISFIED: [(&str, &str); 6] = [
(
"noexecstack",
"every image marks PT_GNU_STACK non-executable",
),
(
"relro",
"every image with a dynamic section carries PT_GNU_RELRO",
),
("lazy", "PLT binding is lazy unless -z now is given"),
(
"notext",
"a relocation into read-only text is refused outright",
),
(
"combreloc",
"dynamic relocations are grouped, RELATIVE first, with DT_RELACOUNT",
),
("nodefaultlib", "no default library path is recorded"),
];
const REFUSED: [(&str, &str); 8] = [
("execstack", "an executable stack is never emitted"),
("norelro", "the RELRO segment is not optional here"),
(
"text",
"a relocation into read-only text is refused, not allowed",
),
("muldefs", "a duplicate strong definition is an error"),
("nocombreloc", "dynamic relocations are always grouped"),
("separate-code", "the segment layout is not configurable"),
("noseparate-code", "the segment layout is not configurable"),
(
"pack-relative-relocs",
"RELR relocations are not implemented",
),
];
pub fn keyword(z: &mut ZOptions, kw: &str) -> Result<(), String> {
match kw {
"now" => z.now = true,
"lazy" => z.now = false,
"origin" => z.origin = true,
"nodelete" => z.nodelete = true,
"nodlopen" => z.nodlopen = true,
"initfirst" => z.initfirst = true,
"interpose" => z.interpose = true,
"global" => z.global = true,
_ => return other(z, kw),
}
Ok(())
}
fn other(z: &mut ZOptions, kw: &str) -> Result<(), String> {
if let Some(value) = kw.strip_prefix("stack-size=") {
z.stack_size = Some(parse_size(value)?);
return Ok(());
}
if SATISFIED.iter().any(|(name, _)| *name == kw) {
return Ok(());
}
if let Some((_, instead)) = REFUSED.iter().find(|(name, _)| *name == kw) {
return Err(format!(
"xold: `-z {kw}` describes an image this linker does not \
produce: {instead}"
));
}
Err(format!(
"xold: unknown `-z {kw}`: the keywords this linker implements are \
now, lazy, origin, nodelete, nodlopen, initfirst, interpose, global \
and stack-size=N, and the ones it already satisfies are {}",
SATISFIED
.iter()
.map(|(name, _)| *name)
.collect::<Vec<_>>()
.join(", ")
))
}
fn parse_size(value: &str) -> Result<u64, String> {
let parsed = value.strip_prefix("0x").map_or_else(
|| value.parse::<u64>().ok(),
|hex| u64::from_str_radix(hex, 16).ok(),
);
parsed.ok_or_else(|| {
format!("xold: `-z stack-size={value}` is not a byte count")
})
}