17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 562b628a6SAli Bahrami * Common Development and Distribution License (the "License"). 662b628a6SAli Bahrami * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22*20c1c355SRod Evans * Copyright (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved. 237c478bd9Sstevel@tonic-gate */ 247c478bd9Sstevel@tonic-gate 257c478bd9Sstevel@tonic-gate /* 267c478bd9Sstevel@tonic-gate * acom: Append Comment 277c478bd9Sstevel@tonic-gate * 287c478bd9Sstevel@tonic-gate * This program demonstrates the use of the libelf interface to 297c478bd9Sstevel@tonic-gate * modify a ELF file. This program will open an ELF file and 307c478bd9Sstevel@tonic-gate * either modify an existing .comment section and/or append 317c478bd9Sstevel@tonic-gate * a new .comment section to an existing ELF file. 327c478bd9Sstevel@tonic-gate */ 337c478bd9Sstevel@tonic-gate 347c478bd9Sstevel@tonic-gate #include <stdio.h> 357c478bd9Sstevel@tonic-gate #include <libelf.h> 367c478bd9Sstevel@tonic-gate #include <gelf.h> 377c478bd9Sstevel@tonic-gate #include <fcntl.h> 387c478bd9Sstevel@tonic-gate #include <unistd.h> 397c478bd9Sstevel@tonic-gate #include <stdlib.h> 407c478bd9Sstevel@tonic-gate #include <string.h> 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate 437c478bd9Sstevel@tonic-gate static const char *CommentStr = ".comment"; 447c478bd9Sstevel@tonic-gate 457c478bd9Sstevel@tonic-gate static void 467c478bd9Sstevel@tonic-gate update_comment(Elf *elf, const char *file, const char *comment) 477c478bd9Sstevel@tonic-gate { 48*20c1c355SRod Evans Elf_Scn *scn = NULL; 497c478bd9Sstevel@tonic-gate GElf_Shdr shdr; 507c478bd9Sstevel@tonic-gate Elf_Data *data; 517c478bd9Sstevel@tonic-gate size_t shstrndx; 527c478bd9Sstevel@tonic-gate 5362b628a6SAli Bahrami if (elf_getshdrstrndx(elf, &shstrndx) == -1) { 5462b628a6SAli Bahrami (void) fprintf(stderr, "%s: gelf_getshdrstrdx() failed: %s\n", 557c478bd9Sstevel@tonic-gate file, elf_errmsg(0)); 567c478bd9Sstevel@tonic-gate return; 577c478bd9Sstevel@tonic-gate } 587c478bd9Sstevel@tonic-gate 59*20c1c355SRod Evans while ((scn = elf_nextscn(elf, scn)) != NULL) { 607c478bd9Sstevel@tonic-gate /* 617c478bd9Sstevel@tonic-gate * Do a string compare to examine each section header 627c478bd9Sstevel@tonic-gate * to see if it is a ".comment" section. If it is then 637c478bd9Sstevel@tonic-gate * this is the section we want to process. 647c478bd9Sstevel@tonic-gate */ 65*20c1c355SRod Evans if (gelf_getshdr(scn, &shdr) == NULL) { 6662b628a6SAli Bahrami (void) fprintf(stderr, "%s: elf_getshdr() failed: %s\n", 677c478bd9Sstevel@tonic-gate file, elf_errmsg(0)); 687c478bd9Sstevel@tonic-gate return; 697c478bd9Sstevel@tonic-gate } 707c478bd9Sstevel@tonic-gate if (strcmp(CommentStr, elf_strptr(elf, shstrndx, 717c478bd9Sstevel@tonic-gate shdr.sh_name)) == 0) 727c478bd9Sstevel@tonic-gate break; 737c478bd9Sstevel@tonic-gate } 747c478bd9Sstevel@tonic-gate 75*20c1c355SRod Evans if (scn == NULL) { 767c478bd9Sstevel@tonic-gate int ndx; 777c478bd9Sstevel@tonic-gate 787c478bd9Sstevel@tonic-gate (void) printf("%s has no .comment section. " 797c478bd9Sstevel@tonic-gate "Creating one...\n", file); 807c478bd9Sstevel@tonic-gate /* 817c478bd9Sstevel@tonic-gate * First add the ".comment" string to the string table 827c478bd9Sstevel@tonic-gate */ 83*20c1c355SRod Evans if ((scn = elf_getscn(elf, shstrndx)) == NULL) { 847c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: elf_getscn() failed: %s\n", 857c478bd9Sstevel@tonic-gate file, elf_errmsg(0)); 867c478bd9Sstevel@tonic-gate return; 877c478bd9Sstevel@tonic-gate } 88*20c1c355SRod Evans if ((data = elf_getdata(scn, NULL)) == NULL) { 897c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: elf_getdata() failed: %s\n", 907c478bd9Sstevel@tonic-gate file, elf_errmsg(0)); 917c478bd9Sstevel@tonic-gate return; 927c478bd9Sstevel@tonic-gate } 937c478bd9Sstevel@tonic-gate ndx = data->d_off + data->d_size; 94*20c1c355SRod Evans if ((data = elf_newdata(scn)) == NULL) { 957c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: elf_newdata() failed: %s\n", 967c478bd9Sstevel@tonic-gate file, elf_errmsg(0)); 977c478bd9Sstevel@tonic-gate return; 987c478bd9Sstevel@tonic-gate } 997c478bd9Sstevel@tonic-gate data->d_buf = (void *)CommentStr; 1007c478bd9Sstevel@tonic-gate data->d_size = strlen(CommentStr) + 1; 1017c478bd9Sstevel@tonic-gate data->d_align = 1; 1027c478bd9Sstevel@tonic-gate 1037c478bd9Sstevel@tonic-gate /* 1047c478bd9Sstevel@tonic-gate * Add the ".comment" section to the end of the file. 1057c478bd9Sstevel@tonic-gate * Initialize the fields in the Section Header that 1067c478bd9Sstevel@tonic-gate * libelf will not fill in. 1077c478bd9Sstevel@tonic-gate */ 108*20c1c355SRod Evans if ((scn = elf_newscn(elf)) == NULL) { 1097c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: elf_newscn() failed: %s\n", 1107c478bd9Sstevel@tonic-gate file, elf_errmsg(0)); 1117c478bd9Sstevel@tonic-gate return; 1127c478bd9Sstevel@tonic-gate } 113*20c1c355SRod Evans if (gelf_getshdr(scn, &shdr) == NULL) { 11462b628a6SAli Bahrami (void) fprintf(stderr, "%s: elf_getshdr() failed: %s\n", 1157c478bd9Sstevel@tonic-gate file, elf_errmsg(0)); 1167c478bd9Sstevel@tonic-gate return; 1177c478bd9Sstevel@tonic-gate } 1187c478bd9Sstevel@tonic-gate shdr.sh_name = ndx; 1197c478bd9Sstevel@tonic-gate shdr.sh_type = SHT_PROGBITS; 1207c478bd9Sstevel@tonic-gate shdr.sh_flags = 0; 1217c478bd9Sstevel@tonic-gate shdr.sh_addr = 0; 1227c478bd9Sstevel@tonic-gate shdr.sh_link = 0; 1237c478bd9Sstevel@tonic-gate shdr.sh_info = 0; 1247c478bd9Sstevel@tonic-gate 1257c478bd9Sstevel@tonic-gate /* 1267c478bd9Sstevel@tonic-gate * Flush the changes to the underlying elf32 or elf64 1277c478bd9Sstevel@tonic-gate * section header. 1287c478bd9Sstevel@tonic-gate */ 129*20c1c355SRod Evans (void) gelf_update_shdr(scn, &shdr); 1307c478bd9Sstevel@tonic-gate } 1317c478bd9Sstevel@tonic-gate 1327c478bd9Sstevel@tonic-gate if (shdr.sh_addr != 0) { 1337c478bd9Sstevel@tonic-gate (void) printf("%s: .comment section is part of a " 1347c478bd9Sstevel@tonic-gate "loadable segment, it cannot be changed.\n", file); 1357c478bd9Sstevel@tonic-gate return; 1367c478bd9Sstevel@tonic-gate } 1377c478bd9Sstevel@tonic-gate 138*20c1c355SRod Evans if ((data = elf_newdata(scn)) == NULL) { 1397c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: elf_getdata() failed: %s\n", 1407c478bd9Sstevel@tonic-gate file, elf_errmsg(0)); 1417c478bd9Sstevel@tonic-gate return; 1427c478bd9Sstevel@tonic-gate } 1437c478bd9Sstevel@tonic-gate data->d_buf = (void *)comment; 1447c478bd9Sstevel@tonic-gate data->d_size = strlen(comment) + 1; 1457c478bd9Sstevel@tonic-gate data->d_align = 1; 1467c478bd9Sstevel@tonic-gate 1477c478bd9Sstevel@tonic-gate if (elf_update(elf, ELF_C_WRITE) == -1) 1487c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: elf_update() failed: %s\n", file, 1497c478bd9Sstevel@tonic-gate elf_errmsg(0)); 1507c478bd9Sstevel@tonic-gate } 1517c478bd9Sstevel@tonic-gate 1527c478bd9Sstevel@tonic-gate int 1537c478bd9Sstevel@tonic-gate main(int argc, char **argv) 1547c478bd9Sstevel@tonic-gate { 1557c478bd9Sstevel@tonic-gate int i; 1567c478bd9Sstevel@tonic-gate char *new_comment; 1577c478bd9Sstevel@tonic-gate 1587c478bd9Sstevel@tonic-gate 1597c478bd9Sstevel@tonic-gate if (argc < 3) { 1607c478bd9Sstevel@tonic-gate (void) printf("usage: %s <new comment> elf_file ...\n", 1617c478bd9Sstevel@tonic-gate argv[0]); 1627c478bd9Sstevel@tonic-gate return (1); 1637c478bd9Sstevel@tonic-gate } 1647c478bd9Sstevel@tonic-gate 1657c478bd9Sstevel@tonic-gate /* 1667c478bd9Sstevel@tonic-gate * Initialize the elf library, must be called before elf_begin() 1677c478bd9Sstevel@tonic-gate * can be called. 1687c478bd9Sstevel@tonic-gate */ 1697c478bd9Sstevel@tonic-gate if (elf_version(EV_CURRENT) == EV_NONE) { 1707c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "elf_version() failed: %s\n", 1717c478bd9Sstevel@tonic-gate elf_errmsg(0)); 1727c478bd9Sstevel@tonic-gate return (1); 1737c478bd9Sstevel@tonic-gate } 1747c478bd9Sstevel@tonic-gate 1757c478bd9Sstevel@tonic-gate /* 1767c478bd9Sstevel@tonic-gate * The new comment is passed in through the command line. 1777c478bd9Sstevel@tonic-gate * This string will be used to update the .comment section of 1787c478bd9Sstevel@tonic-gate * the specified ELF files. 1797c478bd9Sstevel@tonic-gate */ 1807c478bd9Sstevel@tonic-gate new_comment = argv[1]; 1817c478bd9Sstevel@tonic-gate for (i = 2; i < argc; i++) { 1827c478bd9Sstevel@tonic-gate int fd; 1837c478bd9Sstevel@tonic-gate Elf *elf; 1847c478bd9Sstevel@tonic-gate char *elf_fname; 1857c478bd9Sstevel@tonic-gate 1867c478bd9Sstevel@tonic-gate elf_fname = argv[i]; 1877c478bd9Sstevel@tonic-gate if ((fd = open(elf_fname, O_RDWR)) == -1) { 1887c478bd9Sstevel@tonic-gate perror("open"); 1897c478bd9Sstevel@tonic-gate continue; 1907c478bd9Sstevel@tonic-gate } 1917c478bd9Sstevel@tonic-gate 1927c478bd9Sstevel@tonic-gate /* 1937c478bd9Sstevel@tonic-gate * Attempt to open an Elf descriptor Read/Write 1947c478bd9Sstevel@tonic-gate * for each file. 1957c478bd9Sstevel@tonic-gate */ 1967c478bd9Sstevel@tonic-gate if ((elf = elf_begin(fd, ELF_C_RDWR, 0)) == NULL) { 1977c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "elf_begin() failed: %s\n", 1987c478bd9Sstevel@tonic-gate elf_errmsg(0)); 1997c478bd9Sstevel@tonic-gate (void) close(fd); 2007c478bd9Sstevel@tonic-gate continue; 2017c478bd9Sstevel@tonic-gate } 2027c478bd9Sstevel@tonic-gate /* 2037c478bd9Sstevel@tonic-gate * Determine what kind of elf file this is: 2047c478bd9Sstevel@tonic-gate */ 2057c478bd9Sstevel@tonic-gate if (elf_kind(elf) == ELF_K_ELF) 2067c478bd9Sstevel@tonic-gate update_comment(elf, elf_fname, new_comment); 2077c478bd9Sstevel@tonic-gate else 2087c478bd9Sstevel@tonic-gate (void) printf("%s not of type ELF_K_ELF. " 20962b628a6SAli Bahrami "elf_kind == %d\n", elf_fname, elf_kind(elf)); 2107c478bd9Sstevel@tonic-gate 2117c478bd9Sstevel@tonic-gate (void) elf_end(elf); 2127c478bd9Sstevel@tonic-gate (void) close(fd); 2137c478bd9Sstevel@tonic-gate } 2147c478bd9Sstevel@tonic-gate 2157c478bd9Sstevel@tonic-gate return (0); 2167c478bd9Sstevel@tonic-gate } 217