Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
elfio mirror
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Third Party Mirrors
elfio mirror
Commits
06097d96
Commit
06097d96
authored
4 years ago
by
Serge Lamikhov-Center
Browse files
Options
Downloads
Patches
Plain Diff
Use constants for segment locations
parent
7409369f
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
examples/writer/writer.cpp
+30
-8
30 additions, 8 deletions
examples/writer/writer.cpp
with
30 additions
and
8 deletions
examples/writer/writer.cpp
+
30
−
8
View file @
06097d96
...
...
@@ -20,10 +20,28 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/*
* This example shows how to create ELF object file for Linux on x86
*
* Instructions:
* 1. Compile and link this file with ELFIO library
* g++ writer.cpp -o writer
* 2. Execute result file write_obj
* ./writer
* 3. Add executable flag to the output file
* chmod +x hello_x86_64
* 4. Run the result file:
* ./hello_x86_64
*/
#include
<elfio/elfio.hpp>
using
namespace
ELFIO
;
const
Elf64_Addr
CODE_ADDR
=
0x00401000
;
const
Elf_Xword
PAGE_SIZE
=
0x1000
;
const
Elf64_Addr
DATA_ADDR
=
CODE_ADDR
+
PAGE_SIZE
;
int
main
(
void
)
{
elfio
writer
;
...
...
@@ -45,21 +63,24 @@ int main( void )
char
text
[]
=
{
'\xB8'
,
'\x04'
,
'\x00'
,
'\x00'
,
'\x00'
,
// mov eax, 4
'\xBB'
,
'\x01'
,
'\x00'
,
'\x00'
,
'\x00'
,
// mov ebx, 1
'\xB9'
,
'\x00'
,
'\x
2
0'
,
'\x
4
0'
,
'\x00'
,
// mov ecx, msg
'\xB9'
,
'\x00'
,
'\x
0
0'
,
'\x
0
0'
,
'\x00'
,
// mov ecx, msg
'\xBA'
,
'\x0E'
,
'\x00'
,
'\x00'
,
'\x00'
,
// mov edx, 14
'\xCD'
,
'\x80'
,
// int 0x80
'\xB8'
,
'\x01'
,
'\x00'
,
'\x00'
,
'\x00'
,
// mov eax, 1
'\xCD'
,
'\x80'
// int 0x80
};
// Adjust data address for 'msg'
*
(
uint32_t
*
)(
text
+
11
)
=
DATA_ADDR
;
text_sec
->
set_data
(
text
,
sizeof
(
text
)
);
// Create a loadable segment
segment
*
text_seg
=
writer
.
segments
.
add
();
text_seg
->
set_type
(
PT_LOAD
);
text_seg
->
set_virtual_address
(
0x00401000
);
text_seg
->
set_physical_address
(
0x00401000
);
text_seg
->
set_virtual_address
(
CODE_ADDR
);
text_seg
->
set_physical_address
(
CODE_ADDR
);
text_seg
->
set_flags
(
PF_X
|
PF_R
);
text_seg
->
set_align
(
0x1000
);
text_seg
->
set_align
(
PAGE_SIZE
);
// Add code section into program segment
text_seg
->
add_section_index
(
text_sec
->
get_index
(),
...
...
@@ -80,10 +101,10 @@ int main( void )
// Create a read/write segment
segment
*
data_seg
=
writer
.
segments
.
add
();
data_seg
->
set_type
(
PT_LOAD
);
data_seg
->
set_virtual_address
(
0x00402000
);
data_seg
->
set_physical_address
(
0x00402000
);
data_seg
->
set_virtual_address
(
DATA_ADDR
);
data_seg
->
set_physical_address
(
DATA_ADDR
);
data_seg
->
set_flags
(
PF_W
|
PF_R
);
data_seg
->
set_align
(
0x1000
);
data_seg
->
set_align
(
PAGE_SIZE
);
// Add code section into program segment
data_seg
->
add_section_index
(
data_sec
->
get_index
(),
...
...
@@ -93,6 +114,7 @@ int main( void )
section
*
note_sec
=
writer
.
sections
.
add
(
".note"
);
note_sec
->
set_type
(
SHT_NOTE
);
note_sec
->
set_addr_align
(
1
);
note_section_accessor
note_writer
(
writer
,
note_sec
);
note_writer
.
add_note
(
0x01
,
"Created by ELFIO"
,
0
,
0
);
char
descr
[
6
]
=
{
0x31
,
0x32
,
0x33
,
0x34
,
0x35
,
0x36
};
...
...
@@ -103,7 +125,7 @@ int main( void )
// In this example, the code starts at the first address of the
// 'text_seg' segment. Therefore, the start address is set
// to be equal to the segment location
writer
.
set_entry
(
0x00401000
);
writer
.
set_entry
(
CODE_ADDR
);
// Create ELF file
writer
.
save
(
"hello_x86_64"
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment