Skip to content

Commit

Permalink
Add --relocatable-merge-sections
Browse files Browse the repository at this point in the history
  • Loading branch information
rui314 committed Nov 28, 2022
1 parent cf8cce6 commit c2a0ae1
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 2 deletions.
20 changes: 20 additions & 0 deletions docs/mold.1
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,26 @@ Instead of generating an executable or a shared object file, combine
input object files to generate another object file that can be used as
an input to a linker.
.Pp
.It Fl -relocatable-merge-sections
By default,
.Nm
doesn't merge input sections by name when merging input object files into a \
single output object file for
.Fl r .
For example,
.Ar .text.foo
and
.Ar .text.bar
aren't merged for
.Fl r
even though they are merged into
.Ar .text
according to the default section merging rules.
.Pp
This option changes the behavior so that
.Nm
merges input sections by name by the default section merging rules.
.Pp
.It Fl s , Fl -strip-all
Omit
.Li \.symtab
Expand Down
2 changes: 2 additions & 0 deletions elf/cmdline.cc
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,8 @@ std::vector<std::string> parse_nonpositional_args(Context<E> &ctx) {
ctx.arg.relocatable = true;
ctx.arg.emit_relocs = true;
ctx.arg.discard_locals = false;
} else if (read_flag("relocatable-merge-sections")) {
ctx.arg.relocatable_merge_sections = true;
} else if (read_flag("perf")) {
ctx.arg.perf = true;
} else if (read_flag("pack-dyn-relocs=relr")) {
Expand Down
1 change: 1 addition & 0 deletions elf/mold.h
Original file line number Diff line number Diff line change
Expand Up @@ -1660,6 +1660,7 @@ struct Context {
bool quick_exit = true;
bool relax = true;
bool relocatable = false;
bool relocatable_merge_sections = false;
bool repro = false;
bool rosegment = true;
bool shared = false;
Expand Down
4 changes: 3 additions & 1 deletion elf/output-chunks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1857,7 +1857,9 @@ void GnuHashSection<E>::copy_buf(Context<E> &ctx) {
template <typename E>
std::string_view
get_merged_output_name(Context<E> &ctx, std::string_view name, u64 flags) {
if (ctx.arg.relocatable || (ctx.arg.unique && ctx.arg.unique->match(name)))
if (ctx.arg.relocatable && !ctx.arg.relocatable_merge_sections)
return name;
if (ctx.arg.unique && ctx.arg.unique->match(name))
return name;
if (name == ".rodata" || name.starts_with(".rodata."))
return (flags & SHF_STRINGS) ? ".rodata.str" : ".rodata.cst";
Expand Down
2 changes: 1 addition & 1 deletion elf/passes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ struct OutputSectionKey {
template <typename E>
std::string_view
get_output_name(Context<E> &ctx, std::string_view name, u64 flags) {
if (ctx.arg.relocatable)
if (ctx.arg.relocatable && !ctx.arg.relocatable_merge_sections)
return name;
if (ctx.arg.unique && ctx.arg.unique->match(name))
return name;
Expand Down

0 comments on commit c2a0ae1

Please sign in to comment.