0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #include <stdio.h>
0015 #include <stdlib.h>
0016 #include <fcntl.h>
0017 #include <unistd.h>
0018 #include <string.h>
0019
0020
0021 static const char arch[] = "PowerPC";
0022
0023 #define N_DESCR 6
0024 unsigned int descr[N_DESCR] = {
0025 0xffffffff,
0026 0x02000000,
0027 0xffffffff,
0028 0xffffffff,
0029 0xffffffff,
0030 0x4000,
0031 };
0032
0033
0034 static const char rpaname[] = "IBM,RPA-Client-Config";
0035
0036
0037
0038
0039
0040
0041
0042 #define N_RPA_DESCR 8
0043 unsigned int rpanote[N_RPA_DESCR] = {
0044 0,
0045 64,
0046 0,
0047 40,
0048 1,
0049 -1,
0050 0,
0051 1,
0052 };
0053
0054 #define ROUNDUP(len) (((len) + 3) & ~3)
0055
0056 unsigned char buf[1024];
0057 #define ELFDATA2LSB 1
0058 #define ELFDATA2MSB 2
0059 static int e_data = ELFDATA2MSB;
0060 #define ELFCLASS32 1
0061 #define ELFCLASS64 2
0062 static int e_class = ELFCLASS32;
0063
0064 #define GET_16BE(off) ((buf[off] << 8) + (buf[(off)+1]))
0065 #define GET_32BE(off) ((GET_16BE(off) << 16U) + GET_16BE((off)+2U))
0066 #define GET_64BE(off) ((((unsigned long long)GET_32BE(off)) << 32ULL) + \
0067 ((unsigned long long)GET_32BE((off)+4ULL)))
0068 #define PUT_16BE(off, v)(buf[off] = ((v) >> 8) & 0xff, \
0069 buf[(off) + 1] = (v) & 0xff)
0070 #define PUT_32BE(off, v)(PUT_16BE((off), (v) >> 16L), PUT_16BE((off) + 2, (v)))
0071 #define PUT_64BE(off, v)((PUT_32BE((off), (v) >> 32L), \
0072 PUT_32BE((off) + 4, (v))))
0073
0074 #define GET_16LE(off) ((buf[off]) + (buf[(off)+1] << 8))
0075 #define GET_32LE(off) (GET_16LE(off) + (GET_16LE((off)+2U) << 16U))
0076 #define GET_64LE(off) ((unsigned long long)GET_32LE(off) + \
0077 (((unsigned long long)GET_32LE((off)+4ULL)) << 32ULL))
0078 #define PUT_16LE(off, v) (buf[off] = (v) & 0xff, \
0079 buf[(off) + 1] = ((v) >> 8) & 0xff)
0080 #define PUT_32LE(off, v) (PUT_16LE((off), (v)), PUT_16LE((off) + 2, (v) >> 16L))
0081 #define PUT_64LE(off, v) (PUT_32LE((off), (v)), PUT_32LE((off) + 4, (v) >> 32L))
0082
0083 #define GET_16(off) (e_data == ELFDATA2MSB ? GET_16BE(off) : GET_16LE(off))
0084 #define GET_32(off) (e_data == ELFDATA2MSB ? GET_32BE(off) : GET_32LE(off))
0085 #define GET_64(off) (e_data == ELFDATA2MSB ? GET_64BE(off) : GET_64LE(off))
0086 #define PUT_16(off, v) (e_data == ELFDATA2MSB ? PUT_16BE(off, v) : \
0087 PUT_16LE(off, v))
0088 #define PUT_32(off, v) (e_data == ELFDATA2MSB ? PUT_32BE(off, v) : \
0089 PUT_32LE(off, v))
0090 #define PUT_64(off, v) (e_data == ELFDATA2MSB ? PUT_64BE(off, v) : \
0091 PUT_64LE(off, v))
0092
0093
0094 #define E_IDENT 0
0095 #define E_PHOFF (e_class == ELFCLASS32 ? 28 : 32)
0096 #define E_PHENTSIZE (e_class == ELFCLASS32 ? 42 : 54)
0097 #define E_PHNUM (e_class == ELFCLASS32 ? 44 : 56)
0098 #define E_HSIZE (e_class == ELFCLASS32 ? 52 : 64)
0099
0100 #define EI_MAGIC 0
0101 #define EI_CLASS 4
0102 #define EI_DATA 5
0103
0104 #define PH_TYPE 0
0105 #define PH_OFFSET (e_class == ELFCLASS32 ? 4 : 8)
0106 #define PH_FILESZ (e_class == ELFCLASS32 ? 16 : 32)
0107 #define PH_HSIZE (e_class == ELFCLASS32 ? 32 : 56)
0108
0109 #define PT_NOTE 4
0110
0111
0112 unsigned char elf_magic[4] = { 0x7f, 'E', 'L', 'F' };
0113
0114 int
0115 main(int ac, char **av)
0116 {
0117 int fd, n, i;
0118 unsigned long ph, ps, np;
0119 long nnote, nnote2, ns;
0120
0121 if (ac != 2) {
0122 fprintf(stderr, "Usage: %s elf-file\n", av[0]);
0123 exit(1);
0124 }
0125 fd = open(av[1], O_RDWR);
0126 if (fd < 0) {
0127 perror(av[1]);
0128 exit(1);
0129 }
0130
0131 nnote = 12 + ROUNDUP(strlen(arch) + 1) + sizeof(descr);
0132 nnote2 = 12 + ROUNDUP(strlen(rpaname) + 1) + sizeof(rpanote);
0133
0134 n = read(fd, buf, sizeof(buf));
0135 if (n < 0) {
0136 perror("read");
0137 exit(1);
0138 }
0139
0140 if (memcmp(&buf[E_IDENT+EI_MAGIC], elf_magic, 4) != 0)
0141 goto notelf;
0142 e_class = buf[E_IDENT+EI_CLASS];
0143 if (e_class != ELFCLASS32 && e_class != ELFCLASS64)
0144 goto notelf;
0145 e_data = buf[E_IDENT+EI_DATA];
0146 if (e_data != ELFDATA2MSB && e_data != ELFDATA2LSB)
0147 goto notelf;
0148 if (n < E_HSIZE)
0149 goto notelf;
0150
0151 ph = (e_class == ELFCLASS32 ? GET_32(E_PHOFF) : GET_64(E_PHOFF));
0152 ps = GET_16(E_PHENTSIZE);
0153 np = GET_16(E_PHNUM);
0154 if (ph < E_HSIZE || ps < PH_HSIZE || np < 1)
0155 goto notelf;
0156 if (ph + (np + 2) * ps + nnote + nnote2 > n)
0157 goto nospace;
0158
0159 for (i = 0; i < np; ++i) {
0160 if (GET_32(ph + PH_TYPE) == PT_NOTE) {
0161 fprintf(stderr, "%s already has a note entry\n",
0162 av[1]);
0163 exit(0);
0164 }
0165 ph += ps;
0166 }
0167
0168
0169 for (i = 0; i < 2 * ps + nnote + nnote2; ++i)
0170 if (buf[ph + i] != 0)
0171 goto nospace;
0172
0173
0174 ns = ph + 2 * ps;
0175 PUT_32(ph + PH_TYPE, PT_NOTE);
0176 if (e_class == ELFCLASS32)
0177 PUT_32(ph + PH_OFFSET, ns);
0178 else
0179 PUT_64(ph + PH_OFFSET, ns);
0180
0181 if (e_class == ELFCLASS32)
0182 PUT_32(ph + PH_FILESZ, nnote);
0183 else
0184 PUT_64(ph + PH_FILESZ, nnote);
0185
0186
0187
0188 PUT_32(ns, strlen(arch) + 1);
0189 PUT_32(ns + 4, N_DESCR * 4);
0190 PUT_32(ns + 8, 0x1275);
0191 strcpy((char *) &buf[ns + 12], arch);
0192 ns += 12 + strlen(arch) + 1;
0193 for (i = 0; i < N_DESCR; ++i, ns += 4)
0194 PUT_32BE(ns, descr[i]);
0195
0196
0197 ph += ps;
0198 PUT_32(ph + PH_TYPE, PT_NOTE);
0199 if (e_class == ELFCLASS32)
0200 PUT_32(ph + PH_OFFSET, ns);
0201 else
0202 PUT_64(ph + PH_OFFSET, ns);
0203
0204 if (e_class == ELFCLASS32)
0205 PUT_32(ph + PH_FILESZ, nnote);
0206 else
0207 PUT_64(ph + PH_FILESZ, nnote2);
0208
0209
0210 PUT_32(ns, strlen(rpaname) + 1);
0211 PUT_32(ns + 4, sizeof(rpanote));
0212 PUT_32(ns + 8, 0x12759999);
0213 strcpy((char *) &buf[ns + 12], rpaname);
0214 ns += 12 + ROUNDUP(strlen(rpaname) + 1);
0215 for (i = 0; i < N_RPA_DESCR; ++i, ns += 4)
0216 PUT_32BE(ns, rpanote[i]);
0217
0218
0219 PUT_16(E_PHNUM, np + 2);
0220
0221
0222 i = lseek(fd, (long) 0, SEEK_SET);
0223 if (i < 0) {
0224 perror("lseek");
0225 exit(1);
0226 }
0227 i = write(fd, buf, n);
0228 if (i < 0) {
0229 perror("write");
0230 exit(1);
0231 }
0232 if (i < n) {
0233 fprintf(stderr, "%s: write truncated\n", av[1]);
0234 exit(1);
0235 }
0236
0237 exit(0);
0238
0239 notelf:
0240 fprintf(stderr, "%s does not appear to be an ELF file\n", av[1]);
0241 exit(1);
0242
0243 nospace:
0244 fprintf(stderr, "sorry, I can't find space in %s to put the note\n",
0245 av[1]);
0246 exit(1);
0247 }