165042b18Sopenharmony_ci/*
265042b18Sopenharmony_ciCopyright (C) 2001-present by Serge Lamikhov-Center
365042b18Sopenharmony_ci
465042b18Sopenharmony_ciPermission is hereby granted, free of charge, to any person obtaining a copy
565042b18Sopenharmony_ciof this software and associated documentation files (the "Software"), to deal
665042b18Sopenharmony_ciin the Software without restriction, including without limitation the rights
765042b18Sopenharmony_cito use, copy, modify, merge, publish, distribute, sublicense, and/or sell
865042b18Sopenharmony_cicopies of the Software, and to permit persons to whom the Software is
965042b18Sopenharmony_cifurnished to do so, subject to the following conditions:
1065042b18Sopenharmony_ci
1165042b18Sopenharmony_ciThe above copyright notice and this permission notice shall be included in
1265042b18Sopenharmony_ciall copies or substantial portions of the Software.
1365042b18Sopenharmony_ci
1465042b18Sopenharmony_ciTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1565042b18Sopenharmony_ciIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1665042b18Sopenharmony_ciFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1765042b18Sopenharmony_ciAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1865042b18Sopenharmony_ciLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1965042b18Sopenharmony_ciOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2065042b18Sopenharmony_ciTHE SOFTWARE.
2165042b18Sopenharmony_ci*/
2265042b18Sopenharmony_ci
2365042b18Sopenharmony_ci#include <iostream>
2465042b18Sopenharmony_ci#include <elfio/elfio.hpp>
2565042b18Sopenharmony_ci
2665042b18Sopenharmony_ciusing namespace ELFIO;
2765042b18Sopenharmony_ci
2865042b18Sopenharmony_ciint main( int argc, char** argv )
2965042b18Sopenharmony_ci{
3065042b18Sopenharmony_ci    if ( argc != 2 ) {
3165042b18Sopenharmony_ci        std::cout << "Usage: add_section <elf_file>" << std::endl;
3265042b18Sopenharmony_ci        return 1;
3365042b18Sopenharmony_ci    }
3465042b18Sopenharmony_ci
3565042b18Sopenharmony_ci    // Create an elfio reader
3665042b18Sopenharmony_ci    elfio reader;
3765042b18Sopenharmony_ci
3865042b18Sopenharmony_ci    // Load ELF data
3965042b18Sopenharmony_ci    if ( !reader.load( argv[1] ) ) {
4065042b18Sopenharmony_ci        std::cout << "Can't find or process ELF file " << argv[1] << std::endl;
4165042b18Sopenharmony_ci        return 2;
4265042b18Sopenharmony_ci    }
4365042b18Sopenharmony_ci
4465042b18Sopenharmony_ci    // Create additional section at the end of the file
4565042b18Sopenharmony_ci    section* note_sec = reader.sections.add( ".note.ELFIO" );
4665042b18Sopenharmony_ci    note_sec->set_type( SHT_NOTE );
4765042b18Sopenharmony_ci    note_section_accessor note_writer( reader, note_sec );
4865042b18Sopenharmony_ci    note_writer.add_note( 0x01, "Created by ELFIO", "My data", 8 );
4965042b18Sopenharmony_ci
5065042b18Sopenharmony_ci    reader.save( "./result.elf" );
5165042b18Sopenharmony_ci
5265042b18Sopenharmony_ci    return 0;
5365042b18Sopenharmony_ci}
54