Skip to content

Commit 34a065f

Browse files
committed
support omagic
Purpose: --omagic is primarily used when you want to generate an executable with a specific memory layout and characteristics, particularly when memory space is limited or when demand paging is not desired. Key features: No page alignment: The data segment is not aligned to page boundaries, which can lead to more compact executables. Readable and writable sections: Both the text and data sections are marked as readable and writable, allowing for modifications during execution. Fixes (qualcomm#98) Change-Id: I46480d959c41c175a4898c89ec0ac286fde54bcb Signed-off-by: Shankar Easwaran <[email protected]>
1 parent 81d61fc commit 34a065f

File tree

9 files changed

+72
-21
lines changed

9 files changed

+72
-21
lines changed

include/eld/Config/GeneralOptions.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ class GeneralOptions {
302302
// -N, --omagic
303303
void setOMagic(bool PMagic = true) { BOMagic = PMagic; }
304304

305-
bool omagic() const { return BOMagic; }
305+
bool isOMagic() const { return BOMagic; }
306306

307307
// -S, --strip-debug
308308
void setStripDebug(bool PStripDebug = true) { BStripDebug = PStripDebug; }
@@ -1150,8 +1150,8 @@ class GeneralOptions {
11501150
bool BColor = true; // --color[=true,false,auto]
11511151
bool BCreateEhFrameHdr = false; // --eh-frame-hdr
11521152
bool BCreateEhFrameHdrSet = false;
1153-
bool BNMagic = false; // -n, --nmagic
11541153
bool BOMagic = false; // -N, --omagic
1154+
bool BNMagic = false; // -n, --nmagic
11551155
bool BStripDebug = false; // -S, --strip-debug
11561156
bool BExportDynamic = false; //-E, --export-dynamic
11571157
bool BWarnSharedTextrel = false; // --warn-shared-textrel

include/eld/Driver/GnuLinkerOptions.td

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,24 @@ def nostdlib : Flag<["-"], "nostdlib">,
373373
def emit_relocs : Flag<["-", "--"], "emit-relocs">,
374374
HelpText<"Make Emit relocs behave just like GNU.">,
375375
Group<grp_main>;
376+
def omagic
377+
: Flag<["--"], "omagic">,
378+
HelpText<"Set the text and data sections to be readable and writable."
379+
" Also, do not page-align the data segment, and"
380+
" disable linking against shared libraries.">,
381+
Group<grp_main>;
382+
def no_omagic
383+
: Flag<["--"], "no-omagic">,
384+
HelpText<"This option negates most of the effects of the -N option."
385+
"Disable linking with shared libraries">,
386+
Group<grp_main>;
387+
def omagic_alias
388+
: Flag<["-"], "N">,
389+
HelpText<"Set the text and data sections to be readable and writable."
390+
" Also, do not page-align the data segment, and"
391+
" disable linking against shared libraries.">,
392+
Alias<omagic>,
393+
Group<grp_main>;
376394

377395
//===----------------------------------------------------------------------===//
378396
/// Dynamic Library/Executable Options
@@ -942,30 +960,12 @@ def nmagic : Flag<["--"], "nmagic">,
942960
HelpText<"Turn off page alignment of sections,"
943961
" and disable linking against shared libraries">,
944962
Group<grp_compatorignoredopts>;
945-
def omagic
946-
: Flag<["--"], "omagic">,
947-
HelpText<"Set the text and data sections to be readable and writable."
948-
" Also, do not page-align the data segment, and"
949-
" disable linking against shared libraries.">,
950-
Group<grp_compatorignoredopts>;
951-
def no_omagic
952-
: Flag<["--"], "no-omagic">,
953-
HelpText<"This option negates most of the effects of the -N option."
954-
"Disable linking with shared libraries">,
955-
Group<grp_compatorignoredopts>;
956963
// Compatible Aliases
957964
def nmagic_alias : Flag<["-"], "n">,
958965
HelpText<"Turn off page alignment of sections,"
959966
" and disable linking against shared libraries">,
960967
Alias<nmagic>,
961968
Group<grp_compatorignoredopts>;
962-
def omagic_alias
963-
: Flag<["-"], "N">,
964-
HelpText<"Set the text and data sections to be readable and writable."
965-
" Also, do not page-align the data segment, and"
966-
" disable linking against shared libraries.">,
967-
Alias<omagic>,
968-
Group<grp_compatorignoredopts>;
969969
def add_needed : Flag<["--"], "add-needed">,
970970
Group<grp_compatorignoredopts>,
971971
HelpText<"Deprecated">;

lib/Core/Linker.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,11 @@ bool Linker::activateInputs(std::vector<InputAction *> &Actions) {
267267
}
268268

269269
bool Linker::initializeInputTree(std::vector<InputAction *> &Actions) {
270+
271+
// Prefer static libraries over dynamic libraries with omagic
272+
if (ThisConfig->options().isOMagic())
273+
IR->getInputBuilder().makeBStatic();
274+
270275
{
271276
LinkerProgress->incrementAndDisplayProgress();
272277
eld::RegisterTimer T("Input Activation", "Initialize",

lib/LinkerWrapper/GnuLdDriver.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,14 @@ bool GnuLdDriver::processOptions(llvm::opt::InputArgList &Args) {
11481148
if (Args.hasArg(T::noDefaultPlugins))
11491149
Config.options().setNoDefaultPlugins();
11501150

1151+
// --no-omagic, --omagic, -N support
1152+
if (Args.hasArg(T::no_omagic))
1153+
Config.options().setOMagic(false);
1154+
else if (Args.hasArg(T::omagic)) {
1155+
Config.options().setAlignSegments(false);
1156+
Config.options().setOMagic(true);
1157+
}
1158+
11511159
Config.options().setUnknownOptions(Args.getAllArgValues(T::UNKNOWN));
11521160
return true;
11531161
}

lib/Target/CreateProgramHeaders.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,9 +380,12 @@ bool GNULDBackend::createProgramHdrs() {
380380
->getMemoryDescriptor();
381381

382382
// If the user specifies the linker to create a separate rosegment, do that.
383-
if (!config().options().rosegment())
383+
if (!config().options().rosegment() || config().options().isOMagic())
384384
cur_flag = (cur_flag & ~llvm::ELF::PF_X);
385385

386+
if (config().options().isOMagic())
387+
cur_flag = (cur_flag & ~llvm::ELF::PF_W);
388+
386389
// getSegmentFlag returns 0 if the section is not allocatable.
387390
if ((cur_flag != prev_flag) && (isCurAlloc))
388391
createPT_LOAD = true;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const int rodata[] = {1,2,3};
2+
int rwdata = 100;
3+
int foo() { return 0; }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
int bar() { return 0; }
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
SECTIONS {
2+
.text : { *(.text*) }
3+
.rodata : { *(.rodata*) }
4+
.data : { *(.data*) }
5+
/DISCARD/ : { *(.ARM.exidx*) }
6+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#--omagic.test------------------ Executable,LS----------------#
2+
#BEGIN_COMMENT
3+
#Verify that the linker is able to produce output that supports omagic
4+
#END_COMMENT
5+
#START_TEST
6+
7+
# Compile required files
8+
RUN: %clang %clangg0opts -o %t1.1.o -c %p/Inputs/1.c -ffunction-sections
9+
RUN: %clang %clangg0opts -o %t1.2.o -c %p/Inputs/2.c -ffunction-sections
10+
# link with omagic switch
11+
RUN: %link %linkg0opts %t1.1.o %t1.2.o -o %t2.out.omagic --omagic
12+
RUN: %readelf -l -W %t2.out.omagic | %filecheck %s -check-prefix=OMAGIC
13+
# link with no-omagic switch
14+
RUN: %link %linkg0opts %t1.1.o %t1.2.o -o %t2.out.noomagic --omagic --no-omagic
15+
RUN: %readelf -l -W %t2.out.noomagic | %filecheck %s -check-prefix=NOOMAGIC
16+
# link with omagic switch and linker script
17+
RUN: %link %linkg0opts %t1.1.o %t1.2.o -T %p/Inputs/script.t -o %t2.out.omagic.ls --omagic
18+
RUN: %readelf -l -W %t2.out.omagic.ls | %filecheck %s -check-prefix=OMAGICLS
19+
20+
#OMAGIC: .text .rodata {{.*.data}}
21+
#NOOMAGIC: .text .rodata
22+
#NOOMAGIC: .data
23+
#OMAGICLS: .text .rodata .data
24+
25+
#END_TEST

0 commit comments

Comments
 (0)