2 * addnote.c - Program to hack in a PT_NOTE program header entry in an ELF file.
3 * This is needed for OF on RS/6000s to load an image correctly.
4 * Note that OF needs a program header entry for the note, not an
7 * Copyright 2000 Paul Mackerras.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 /* Usage: addnote zImage */
31 char arch[] = "PowerPC";
34 unsigned int descr[N_DESCR] = {
35 0xffffffff, /* real-mode = true */
36 0x00c00000, /* real-base, i.e. where we expect OF to be */
37 0xffffffff, /* real-size */
38 0xffffffff, /* virt-base */
39 0xffffffff, /* virt-size */
40 0x4000, /* load-base */
43 unsigned char buf[512];
45 #define GET_16BE(off) ((buf[off] << 8) + (buf[(off)+1]))
46 #define GET_32BE(off) ((GET_16BE(off) << 16) + GET_16BE((off)+2))
47 #define PUT_16BE(off, v) (buf[off] = ((v) >> 8) & 0xff, \
48 buf[(off) + 1] = (v) & 0xff)
49 #define PUT_32BE(off, v) (PUT_16BE((off), (v) >> 16), \
50 PUT_16BE((off) + 2, (v)))
52 /* Structure of an ELF file */
53 #define E_IDENT 0 /* ELF header */
55 #define E_PHENTSIZE 42
57 #define E_HSIZE 52 /* size of ELF header */
59 #define EI_MAGIC 0 /* offsets in E_IDENT area */
63 #define PH_TYPE 0 /* ELF program header */
66 #define PH_HSIZE 32 /* size of program header */
68 #define PT_NOTE 4 /* Program header type = note */
73 unsigned char elf_magic[4] = { 0x7f, 'E', 'L', 'F' };
76 main(int ac, char **av)
83 fprintf(stderr, "Usage: %s elf-file\n", av[0]);
86 fd = open(av[1], O_RDWR);
92 nnote = strlen(arch) + 1 + (N_DESCR + 3) * 4;
94 n = read(fd, buf, sizeof(buf));
100 if (n < E_HSIZE || memcmp(&buf[E_IDENT+EI_MAGIC], elf_magic, 4) != 0)
103 if (buf[E_IDENT+EI_CLASS] != ELFCLASS32
104 || buf[E_IDENT+EI_DATA] != ELFDATA2MSB) {
105 fprintf(stderr, "%s is not a big-endian 32-bit ELF image\n",
110 ph = GET_32BE(E_PHOFF);
111 ps = GET_16BE(E_PHENTSIZE);
112 np = GET_16BE(E_PHNUM);
113 if (ph < E_HSIZE || ps < PH_HSIZE || np < 1)
115 if (ph + (np + 1) * ps + nnote > n)
118 for (i = 0; i < np; ++i) {
119 if (GET_32BE(ph + PH_TYPE) == PT_NOTE) {
120 fprintf(stderr, "%s already has a note entry\n",
127 /* XXX check that the area we want to use is all zeroes */
128 for (i = 0; i < ps + nnote; ++i)
129 if (buf[ph + i] != 0)
132 /* fill in the program header entry */
134 PUT_32BE(ph + PH_TYPE, PT_NOTE);
135 PUT_32BE(ph + PH_OFFSET, ns);
136 PUT_32BE(ph + PH_FILESZ, nnote);
138 /* fill in the note area we point to */
139 /* XXX we should probably make this a proper section */
140 PUT_32BE(ns, strlen(arch) + 1);
141 PUT_32BE(ns + 4, N_DESCR * 4);
142 PUT_32BE(ns + 8, 0x1275);
143 strcpy(&buf[ns + 12], arch);
144 ns += 12 + strlen(arch) + 1;
145 for (i = 0; i < N_DESCR; ++i)
146 PUT_32BE(ns + i * 4, descr[i]);
148 /* Update the number of program headers */
149 PUT_16BE(E_PHNUM, np + 1);
152 lseek(fd, (long) 0, SEEK_SET);
153 i = write(fd, buf, n);
159 fprintf(stderr, "%s: write truncated\n", av[1]);
166 fprintf(stderr, "%s does not appear to be an ELF file\n", av[1]);
170 fprintf(stderr, "sorry, I can't find space in %s to put the note\n",
177 * c-file-style: "k&r"