1
1
mirror of https://github.com/rui314/mold.git synced 2024-09-22 02:20:51 +03:00

Add -z keep-text-section-prefix

This commit is contained in:
Rui Ueyama 2021-06-15 23:26:18 +09:00
parent 1317132548
commit e7019ebf86
5 changed files with 42 additions and 10 deletions

View File

@ -128,6 +128,8 @@ Options:
-z norelro
-z defs Report undefined symbols (even with --shared)
-z nodefs
-z keep-text-section-prefix Keep .text.{hot,unknown,unlikely,startup,exit} as separate sections in the final binary
-z nokeep-text-section-prefix
-z nodlopen Mark DSO not available to dlopen
-z nodelete Mark DSO non-deletable at runtime
-z nocopyreloc Do not create copy relocations
@ -593,6 +595,10 @@ void parse_nonpositional_args(Context<E> &ctx,
ctx.arg.z_interpose = true;
} else if (read_z_flag(args, "muldefs")) {
ctx.arg.allow_multiple_definition = true;
} else if (read_z_flag(args, "keep-text-section-prefix")) {
ctx.arg.z_keep_text_section_prefix = true;
} else if (read_z_flag(args, "nokeep-text-section-prefix")) {
ctx.arg.z_keep_text_section_prefix = false;
} else if (read_flag(args, "no-undefined")) {
ctx.arg.z_defs = true;
} else if (read_flag(args, "fatal-warnings")) {

View File

@ -593,6 +593,13 @@ variables reside) are not executable for security reasons. \fB\-z
execstack\fR makes it executable. \fB\-z noexecstack\fR restores the
default behavior.
.IP "\fB\-z keep\-text\-section\-prefix\fR"
.PD 0
.IP "\fB\-z nokeep\-text\-section\-prefix\fR"
.PD
Keep .text.hot, .text.unknown, .text.unlikely, .text.startup
and .text.exit as separate sections in the final binary.
.IP "\fB\-z relro\fR"
.PD 0
.IP "\fB\-z norelro\fR"

5
mold.h
View File

@ -1462,11 +1462,10 @@ struct Context {
bool z_execstack = false;
bool z_initfirst = false;
bool z_interpose = false;
bool z_keep_text_section_prefix = false;
bool z_now = false;
bool z_relro = true;
i16 default_version = VER_NDX_GLOBAL;
std::vector<std::string_view> version_definitions;
std::vector<VersionPattern> version_patterns;
i64 filler = -1;
i64 spare_dynamic_tags = 5;
i64 thread_count = -1;
@ -1483,12 +1482,14 @@ struct Context {
std::string sysroot;
std::unique_ptr<std::unordered_set<std::string_view>> retain_symbols_file;
std::unordered_set<std::string_view> wrap;
std::vector<VersionPattern> version_patterns;
std::vector<std::string_view> auxiliary;
std::vector<std::string_view> exclude_libs;
std::vector<std::string_view> filter;
std::vector<std::string_view> library_paths;
std::vector<std::string_view> trace_symbol;
std::vector<std::string_view> undefined;
std::vector<std::string_view> version_definitions;
u64 image_base = 0x200000;
} arg;

View File

@ -553,12 +553,24 @@ void DynamicSection<E>::copy_buf(Context<E> &ctx) {
write_vector(ctx.buf + this->shdr.sh_offset, contents);
}
static std::string_view get_output_name(std::string_view name) {
template <typename E>
static std::string_view get_output_name(Context<E> &ctx, std::string_view name) {
if (ctx.arg.z_keep_text_section_prefix) {
static std::string_view text_prefixes[] = {
".text.hot.", ".text.unknown.", ".text.unlikely.", ".text.startup.",
".text.exit."
};
for (std::string_view prefix : text_prefixes) {
std::string_view stem = prefix.substr(0, prefix.size() - 1);
if (name == stem || name.starts_with(prefix))
return stem;
}
}
static std::string_view prefixes[] = {
".text.hot.", ".text.unknown.", ".text.unlikely.", ".text.startup.",
".text.exit.", ".text.", ".data.rel.ro.", ".data.", ".rodata.",
".bss.rel.ro.", ".bss.", ".init_array.", ".fini_array.", ".tbss.",
".tdata.", ".gcc_except_table.",
".text.", ".data.rel.ro.", ".data.", ".rodata.", ".bss.rel.ro.", ".bss.",
".init_array.", ".fini_array.", ".tbss.", ".tdata.", ".gcc_except_table.",
};
for (std::string_view prefix : prefixes) {
@ -618,7 +630,7 @@ template <typename E>
OutputSection<E> *
OutputSection<E>::get_instance(Context<E> &ctx, std::string_view name,
u64 type, u64 flags) {
name = get_output_name(name);
name = get_output_name(ctx, name);
type = canonicalize_type(name, type);
flags = flags & ~(u64)SHF_GROUP & ~(u64)SHF_COMPRESSED;
@ -1103,7 +1115,7 @@ template <typename E>
MergedSection<E> *
MergedSection<E>::get_instance(Context<E> &ctx, std::string_view name,
u64 type, u64 flags) {
name = get_output_name(name);
name = get_output_name(ctx, name);
flags = flags & ~(u64)SHF_MERGE & ~(u64)SHF_STRINGS;
auto find = [&]() -> MergedSection * {

View File

@ -62,7 +62,7 @@ _start:
.ascii ".gcc_except_table.foo "
EOF
../mold -o $t/exe $t/a.o
../mold -o $t/exe $t/a.o -z keep-text-section-prefix
readelf -p .text.hot $t/exe | fgrep -q '.text.hot .text.hot.foo'
readelf -p .text.unknown $t/exe | fgrep -q '.text.unknown .text.unknown.foo'
@ -75,4 +75,10 @@ readelf -p .data $t/exe | fgrep -q '.data .data.foo'
readelf -p .rodata $t/exe | fgrep -q '.rodata .rodata.foo'
readelf -p .gcc_except_table $t/exe | fgrep -q '.gcc_except_table .gcc_except_table.foo'
../mold -o $t/exe $t/a.o
! readelf --sections $t/exe | fgrep -q .text.hot || false
../mold -o $t/exe $t/a.o -z nokeep-text-section-prefix
! readelf --sections $t/exe | fgrep -q .text.hot || false
echo OK