1/* 2 * BK Id: SCCS/s.addnote.c 1.7 05/18/01 15:17:23 cort 3 */ 4/* 5 * Program to hack in a PT_NOTE program header entry in an ELF file. 6 * This is needed for OF on RS/6000s to load an image correctly. 7 * Note that OF needs a program header entry for the note, not an 8 * ELF section. 9 * 10 * Copyright 2000 Paul Mackerras. 11 * 12 * This program is free software; you can redistribute it and/or 13 * modify it under the terms of the GNU General Public License 14 * as published by the Free Software Foundation; either version 15 * 2 of the License, or (at your option) any later version. 16 * 17 * Usage: addnote zImage 18 */ 19#include <stdio.h> 20#include <fcntl.h> 21#include <unistd.h> 22#include <string.h> 23 24char arch[] = "PowerPC"; 25 26#define N_DESCR 6 27unsigned int descr[N_DESCR] = { 28 /* values for IBM RS/6000 machines */ 29 0xffffffff, /* real-mode = true */ 30 0x00c00000, /* real-base, i.e. where we expect OF to be */ 31 0xffffffff, /* real-size */ 32 0xffffffff, /* virt-base */ 33 0xffffffff, /* virt-size */ 34 0x4000, /* load-base */ 35}; 36 37unsigned char buf[512]; 38 39#define GET_16BE(off) ((buf[off] << 8) + (buf[(off)+1])) 40#define GET_32BE(off) ((GET_16BE(off) << 16) + GET_16BE((off)+2)) 41 42#define PUT_16BE(off, v) (buf[off] = ((v) >> 8) & 0xff, \ 43 buf[(off) + 1] = (v) & 0xff) 44#define PUT_32BE(off, v) (PUT_16BE((off), (v) >> 16), \ 45 PUT_16BE((off) + 2, (v))) 46 47/* Structure of an ELF file */ 48#define E_IDENT 0 /* ELF header */ 49#define E_PHOFF 28 50#define E_PHENTSIZE 42 51#define E_PHNUM 44 52#define E_HSIZE 52 /* size of ELF header */ 53 54#define EI_MAGIC 0 /* offsets in E_IDENT area */ 55#define EI_CLASS 4 56#define EI_DATA 5 57 58#define PH_TYPE 0 /* ELF program header */ 59#define PH_OFFSET 4 60#define PH_FILESZ 16 61#define PH_HSIZE 32 /* size of program header */ 62 63#define PT_NOTE 4 /* Program header type = note */ 64 65#define ELFCLASS32 1 66#define ELFDATA2MSB 2 67 68unsigned char elf_magic[4] = { 0x7f, 'E', 'L', 'F' }; 69 70int main(int ac, char **av) 71{ 72 int fd, n, i; 73 int ph, ps, np; 74 int nnote, ns; 75 76 if (ac != 2) { 77 fprintf(stderr, "Usage: %s elf-file\n", av[0]); 78 exit(1); 79 } 80 fd = open(av[1], O_RDWR); 81 if (fd < 0) { 82 perror(av[1]); 83 exit(1); 84 } 85 86 nnote = strlen(arch) + 1 + (N_DESCR + 3) * 4; 87 88 n = read(fd, buf, sizeof(buf)); 89 if (n < 0) { 90 perror("read"); 91 exit(1); 92 } 93 94 if (n < E_HSIZE || memcmp(&buf[E_IDENT+EI_MAGIC], elf_magic, 4) != 0) 95 goto notelf; 96 97 if (buf[E_IDENT+EI_CLASS] != ELFCLASS32 98 || buf[E_IDENT+EI_DATA] != ELFDATA2MSB) { 99 fprintf(stderr, "%s is not a big-endian 32-bit ELF image\n", 100 av[1]); 101 exit(1); 102 } 103 104 ph = GET_32BE(E_PHOFF); 105 ps = GET_16BE(E_PHENTSIZE); 106 np = GET_16BE(E_PHNUM); 107 if (ph < E_HSIZE || ps < PH_HSIZE || np < 1) 108 goto notelf; 109 if (ph + (np + 1) * ps + nnote > n) 110 goto nospace; 111 112 for (i = 0; i < np; ++i) { 113 if (GET_32BE(ph + PH_TYPE) == PT_NOTE) { 114 fprintf(stderr, "%s already has a note entry\n", 115 av[1]); 116 exit(0); 117 } 118 ph += ps; 119 } 120 121 for (i = 0; i < ps + nnote; ++i) 122 if (buf[ph + i] != 0) 123 goto nospace; 124 125 /* fill in the program header entry */ 126 ns = ph + ps; 127 PUT_32BE(ph + PH_TYPE, PT_NOTE); 128 PUT_32BE(ph + PH_OFFSET, ns); 129 PUT_32BE(ph + PH_FILESZ, nnote); 130 131 /* fill in the note area we point to */ 132 PUT_32BE(ns, strlen(arch) + 1); 133 PUT_32BE(ns + 4, N_DESCR * 4); 134 PUT_32BE(ns + 8, 0x1275); 135 strcpy(&buf[ns + 12], arch); 136 ns += 12 + strlen(arch) + 1; 137 for (i = 0; i < N_DESCR; ++i) 138 PUT_32BE(ns + i * 4, descr[i]); 139 140 /* Update the number of program headers */ 141 PUT_16BE(E_PHNUM, np + 1); 142 143 /* write back */ 144 lseek(fd, (long) 0, SEEK_SET); 145 i = write(fd, buf, n); 146 if (i < 0) { 147 perror("write"); 148 exit(1); 149 } 150 if (i < n) { 151 fprintf(stderr, "%s: write truncated\n", av[1]); 152 exit(1); 153 } 154 155 exit(0); 156 157 notelf: 158 fprintf(stderr, "%s does not appear to be an ELF file\n", av[0]); 159 exit(1); 160 161 nospace: 162 fprintf(stderr, "sorry, I can't find space in %s to put the note\n", 163 av[0]); 164 exit(1); 165} 166