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