From 85c937eba9be3a41768d4e6ecdc149c40ed26353 Mon Sep 17 00:00:00 2001
From: Serge Lamikhov-Center <to_serge@users.sourceforge.net>
Date: Tue, 6 Oct 2020 09:51:14 +0300
Subject: [PATCH] Demonstrate addition of a new section to existing ELF file

---
 .vscode/c_cpp_properties.json        |  1 +
 CMakeLists.txt                       |  1 +
 examples/add_section/CMakeLists.txt  |  3 +++
 examples/add_section/add_section.cpp | 31 ++++++++++++++++++++++++++++
 4 files changed, 36 insertions(+)
 create mode 100644 examples/add_section/CMakeLists.txt
 create mode 100644 examples/add_section/add_section.cpp

diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json
index 61be34e..f54b91d 100644
--- a/.vscode/c_cpp_properties.json
+++ b/.vscode/c_cpp_properties.json
@@ -3,6 +3,7 @@
         {
             "name": "Linux",
             "includePath": [
+                "${workspaceFolder}",
                 "${workspaceFolder}/**"
             ],
             "defines": [],
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 0eabbe6..67513e8 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 3.12.4)
 project(elfio)
 set(CMAKE_CXX_STANDARD 17)
 
+add_subdirectory(examples/add_section)
 add_subdirectory(examples/anonymizer)
 add_subdirectory(examples/elfdump)
 add_subdirectory(examples/tutorial)
diff --git a/examples/add_section/CMakeLists.txt b/examples/add_section/CMakeLists.txt
new file mode 100644
index 0000000..66ce3dd
--- /dev/null
+++ b/examples/add_section/CMakeLists.txt
@@ -0,0 +1,3 @@
+
+add_executable(add_section add_section.cpp)
+include_directories(SYSTEM ${CMAKE_SOURCE_DIR})
diff --git a/examples/add_section/add_section.cpp b/examples/add_section/add_section.cpp
new file mode 100644
index 0000000..a564bd9
--- /dev/null
+++ b/examples/add_section/add_section.cpp
@@ -0,0 +1,31 @@
+#include <iostream>
+#include <elfio/elfio.hpp>
+
+using namespace ELFIO;
+
+int main( int argc, char** argv )
+{
+    if ( argc != 2 ) {
+        std::cout << "Usage: add_section <elf_file>" << std::endl;
+        return 1;
+    }
+
+    // Create an elfio reader
+    elfio reader;
+
+    // Load ELF data
+    if ( !reader.load( argv[1] ) ) {
+        std::cout << "Can't find or process ELF file " << argv[1] << std::endl;
+        return 2;
+    }
+
+    // Create additional section at the end of the file
+    section* note_sec = reader.sections.add( ".note.ELFIO" );
+    note_sec->set_type( SHT_NOTE );
+    note_section_accessor note_writer( reader, note_sec );
+    note_writer.add_note( 0x01, "Created by ELFIO", "My data", 8 );
+
+    reader.save( "./result.elf" );
+
+    return 0;
+}
-- 
GitLab