Skip to content

Commit 98e1110

Browse files
Pasha-From-Russiaserge1
authored andcommitted
Added <elfio-ldd> tool to examples. Show binary file dependencies only
1 parent 9b54e9a commit 98e1110

File tree

4 files changed

+82
-12
lines changed

4 files changed

+82
-12
lines changed

elfio/elfio_dump.hpp

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ static const struct note_tag_t
636636
{ { NT_SPU, "NT_SPU", "" }
637637
} },
638638
{ "GNU",
639-
{
639+
{
640640
{ NT_GNU_ABI_TAG, "NT_GNU_ABI_TAG", "GNU ABI tag" },
641641
{ NT_GNU_HWCAP, "NT_GNU_HWCAP", "Used by ld.so and kernel vDSO" },
642642
{ NT_GNU_BUILD_ID, "NT_GNU_BUILD_ID", "Build ID of the binary" },
@@ -862,7 +862,7 @@ class dump
862862
}
863863
else { // Output for 64-bit
864864
out << "[" << DUMP_DEC_FORMAT( 5 ) << no << "] "
865-
<< DUMP_STR_FORMAT( 14 ) << str_segment_type( seg->get_type() ) << " "
865+
<< DUMP_STR_FORMAT( 14 ) << str_segment_type( seg->get_type() ) << " "
866866
<< DUMP_HEX0x_FORMAT( 16 ) << seg->get_offset() << " "
867867
<< DUMP_HEX0x_FORMAT( 16 ) << seg->get_virtual_address() << " "
868868
<< DUMP_HEX0x_FORMAT( 16 ) << seg->get_physical_address()
@@ -871,7 +871,7 @@ class dump
871871
<< DUMP_HEX0x_FORMAT( 16 ) << seg->get_file_size() << " "
872872
<< DUMP_HEX0x_FORMAT( 16 ) << seg->get_memory_size() << " "
873873
<< DUMP_STR_FORMAT( 3 ) << str_segment_flag( seg->get_flags() ) << " "
874-
<< DUMP_HEX0x_FORMAT( 1 ) << seg->get_align()
874+
<< DUMP_HEX0x_FORMAT( 1 ) << seg->get_align()
875875
<< std::endl;
876876
}
877877
// clang-format on
@@ -1100,7 +1100,7 @@ class dump
11001100

11011101
//------------------------------------------------------------------------------
11021102
// Dumps the dynamic tags information
1103-
static void dynamic_tags( std::ostream& out, const elfio& reader )
1103+
static void dynamic_tags( std::ostream& out, const elfio& reader, bool name_only = false )
11041104
{
11051105
for ( const auto& sec : reader.sections ) { // For all sections
11061106
if ( SHT_DYNAMIC == sec->get_type() ) {
@@ -1109,22 +1109,24 @@ class dump
11091109
Elf_Xword dyn_no = dynamic.get_entries_num();
11101110
if ( dyn_no == 0 )
11111111
continue;
1112-
1113-
out << "Dynamic section (" << sec->get_name() << ")"
1114-
<< std::endl;
1115-
out << "[ Nr ] Tag Name/Value" << std::endl;
1112+
if ( !name_only ) {
1113+
out << "Dynamic section (" << sec->get_name() << ")"
1114+
<< std::endl;
1115+
out << "[ Nr ] Tag Name/Value" << std::endl;
1116+
}
11161117
for ( Elf_Xword i = 0; i < dyn_no; ++i ) {
11171118
Elf_Xword tag = 0;
11181119
Elf_Xword value = 0;
11191120
std::string str;
11201121
dynamic.get_entry( i, tag, value, str );
1121-
dynamic_tag( out, i, tag, value, str, reader.get_class() );
1122+
dynamic_tag( out, i, tag, value, str, reader.get_class(), name_only );
11221123
if ( DT_NULL == tag ) {
11231124
break;
11241125
}
11251126
}
1126-
1127-
out << std::endl;
1127+
if ( !name_only ) {
1128+
out << std::endl;
1129+
}
11281130
}
11291131
}
11301132
}
@@ -1136,8 +1138,16 @@ class dump
11361138
Elf_Xword tag,
11371139
Elf_Xword value,
11381140
const std::string& str,
1139-
unsigned int /*elf_class*/ )
1141+
unsigned int /*elf_class*/,
1142+
bool name_only = false )
11401143
{
1144+
if ( name_only ) {
1145+
if ( str.empty() || tag != DT_NEEDED ) {
1146+
return;
1147+
}
1148+
out << str << std::endl;
1149+
return;
1150+
}
11411151
out << "[" << DUMP_DEC_FORMAT( 5 ) << no << "] "
11421152
<< DUMP_STR_FORMAT( 16 ) << str_dynamic_tag( tag ) << " ";
11431153
if ( str.empty() ) {

examples/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
add_subdirectory(add_section)
22
add_subdirectory(anonymizer)
33
add_subdirectory(elfdump)
4+
add_subdirectory(elfio_ldd)
45
add_subdirectory(proc_mem)
56
add_subdirectory(tutorial)
67
add_subdirectory(write_obj)

examples/elfio_ldd/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
add_executable(elfio-ldd elfio_ldd.cpp)
3+
target_link_libraries(elfio-ldd PRIVATE elfio::elfio)

examples/elfio_ldd/elfio_ldd.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
elfio_ldd.cpp - Use ELFIO library to list dynamic libraries required by ELF file.
3+
4+
2025 Pasha-From-Russia
5+
6+
ELFIO Copyright (C) 2001-present by Serge Lamikhov-Center
7+
8+
Permission is hereby granted, free of charge, to any person obtaining a copy
9+
of this software and associated documentation files (the "Software"), to deal
10+
in the Software without restriction, including without limitation the rights
11+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
copies of the Software, and to permit persons to whom the Software is
13+
furnished to do so, subject to the following conditions:
14+
15+
The above copyright notice and this permission notice shall be included in
16+
all copies or substantial portions of the Software.
17+
18+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
THE SOFTWARE.
25+
*/
26+
27+
#ifdef _MSC_VER
28+
#define _SCL_SECURE_NO_WARNINGS
29+
#define ELFIO_NO_INTTYPES
30+
#endif
31+
32+
#include <iostream>
33+
#include <elfio/elfio_dump.hpp>
34+
35+
using namespace ELFIO;
36+
37+
static const bool kPrintNamesOnly = true;
38+
39+
int main( int argc, char** argv )
40+
{
41+
if ( argc != 2 ) {
42+
printf( "Usage: elfio_ldd <file_name>\n" );
43+
return 1;
44+
}
45+
46+
elfio reader;
47+
48+
if ( !reader.load( argv[1] ) ) {
49+
printf( "File %s is not found or it is not an ELF file\n", argv[1] );
50+
return 1;
51+
}
52+
53+
dump::dynamic_tags( std::cout, reader, kPrintNamesOnly );
54+
55+
return 0;
56+
}

0 commit comments

Comments
 (0)