Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Program to hack in a PT_NOTE program header entry in an ELF file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * This is needed for OF on RS/6000s to load an image correctly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Note that OF needs a program header entry for the note, not an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * ELF section.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Copyright 2000 Paul Mackerras.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * Adapted for 64 bit little endian images by Andrew Tauferner.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * Usage: addnote zImage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) /* CHRP note section */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) static const char arch[] = "PowerPC";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #define N_DESCR	6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) unsigned int descr[N_DESCR] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	0xffffffff,		/* real-mode = true */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	0x02000000,		/* real-base, i.e. where we expect OF to be */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	0xffffffff,		/* real-size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	0xffffffff,		/* virt-base */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	0xffffffff,		/* virt-size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	0x4000,			/* load-base */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) /* RPA note section */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) static const char rpaname[] = "IBM,RPA-Client-Config";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  * Note: setting ignore_my_client_config *should* mean that OF ignores
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)  * all the other fields, but there is a firmware bug which means that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39)  * it looks at the splpar field at least.  So these values need to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)  * reasonable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #define N_RPA_DESCR	8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) unsigned int rpanote[N_RPA_DESCR] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	0,			/* lparaffinity */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	64,			/* min_rmo_size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	0,			/* min_rmo_percent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	40,			/* max_pft_size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	1,			/* splpar */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	-1,			/* min_load */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	0,			/* new_mem_def */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	1,			/* ignore_my_client_config */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) #define ROUNDUP(len)	(((len) + 3) & ~3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) unsigned char buf[1024];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) #define ELFDATA2LSB     1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) #define ELFDATA2MSB     2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) static int e_data = ELFDATA2MSB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) #define ELFCLASS32      1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) #define ELFCLASS64      2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) static int e_class = ELFCLASS32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) #define GET_16BE(off)	((buf[off] << 8) + (buf[(off)+1]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) #define GET_32BE(off)	((GET_16BE(off) << 16U) + GET_16BE((off)+2U))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) #define GET_64BE(off)	((((unsigned long long)GET_32BE(off)) << 32ULL) + \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 			((unsigned long long)GET_32BE((off)+4ULL)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) #define PUT_16BE(off, v)(buf[off] = ((v) >> 8) & 0xff, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 			 buf[(off) + 1] = (v) & 0xff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) #define PUT_32BE(off, v)(PUT_16BE((off), (v) >> 16L), PUT_16BE((off) + 2, (v)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) #define PUT_64BE(off, v)((PUT_32BE((off), (v) >> 32L), \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 			  PUT_32BE((off) + 4, (v))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) #define GET_16LE(off)	((buf[off]) + (buf[(off)+1] << 8))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) #define GET_32LE(off)	(GET_16LE(off) + (GET_16LE((off)+2U) << 16U))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) #define GET_64LE(off)	((unsigned long long)GET_32LE(off) + \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 			(((unsigned long long)GET_32LE((off)+4ULL)) << 32ULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) #define PUT_16LE(off, v) (buf[off] = (v) & 0xff, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 			  buf[(off) + 1] = ((v) >> 8) & 0xff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) #define PUT_32LE(off, v) (PUT_16LE((off), (v)), PUT_16LE((off) + 2, (v) >> 16L))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) #define PUT_64LE(off, v) (PUT_32LE((off), (v)), PUT_32LE((off) + 4, (v) >> 32L))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) #define GET_16(off)	(e_data == ELFDATA2MSB ? GET_16BE(off) : GET_16LE(off))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) #define GET_32(off)	(e_data == ELFDATA2MSB ? GET_32BE(off) : GET_32LE(off))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) #define GET_64(off)	(e_data == ELFDATA2MSB ? GET_64BE(off) : GET_64LE(off))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) #define PUT_16(off, v)	(e_data == ELFDATA2MSB ? PUT_16BE(off, v) : \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 			 PUT_16LE(off, v))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) #define PUT_32(off, v)  (e_data == ELFDATA2MSB ? PUT_32BE(off, v) : \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 			 PUT_32LE(off, v))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) #define PUT_64(off, v)  (e_data == ELFDATA2MSB ? PUT_64BE(off, v) : \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 			 PUT_64LE(off, v))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) /* Structure of an ELF file */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) #define E_IDENT		0	/* ELF header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) #define	E_PHOFF		(e_class == ELFCLASS32 ? 28 : 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) #define E_PHENTSIZE	(e_class == ELFCLASS32 ? 42 : 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) #define E_PHNUM		(e_class == ELFCLASS32 ? 44 : 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) #define E_HSIZE		(e_class == ELFCLASS32 ? 52 : 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) #define EI_MAGIC	0	/* offsets in E_IDENT area */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) #define EI_CLASS	4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) #define EI_DATA		5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) #define PH_TYPE		0	/* ELF program header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) #define PH_OFFSET	(e_class == ELFCLASS32 ? 4 : 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) #define PH_FILESZ	(e_class == ELFCLASS32 ? 16 : 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) #define PH_HSIZE	(e_class == ELFCLASS32 ? 32 : 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) #define PT_NOTE		4	/* Program header type = note */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) unsigned char elf_magic[4] = { 0x7f, 'E', 'L', 'F' };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) main(int ac, char **av)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	int fd, n, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	unsigned long ph, ps, np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	long nnote, nnote2, ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	if (ac != 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		fprintf(stderr, "Usage: %s elf-file\n", av[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	fd = open(av[1], O_RDWR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	if (fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		perror(av[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	nnote = 12 + ROUNDUP(strlen(arch) + 1) + sizeof(descr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	nnote2 = 12 + ROUNDUP(strlen(rpaname) + 1) + sizeof(rpanote);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	n = read(fd, buf, sizeof(buf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	if (n < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		perror("read");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	if (memcmp(&buf[E_IDENT+EI_MAGIC], elf_magic, 4) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		goto notelf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	e_class = buf[E_IDENT+EI_CLASS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	if (e_class != ELFCLASS32 && e_class != ELFCLASS64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		goto notelf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	e_data = buf[E_IDENT+EI_DATA];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	if (e_data != ELFDATA2MSB && e_data != ELFDATA2LSB)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		goto notelf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	if (n < E_HSIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		goto notelf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	ph = (e_class == ELFCLASS32 ? GET_32(E_PHOFF) : GET_64(E_PHOFF));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	ps = GET_16(E_PHENTSIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	np = GET_16(E_PHNUM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	if (ph < E_HSIZE || ps < PH_HSIZE || np < 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		goto notelf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	if (ph + (np + 2) * ps + nnote + nnote2 > n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		goto nospace;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	for (i = 0; i < np; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		if (GET_32(ph + PH_TYPE) == PT_NOTE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 			fprintf(stderr, "%s already has a note entry\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 				av[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 			exit(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		ph += ps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	/* XXX check that the area we want to use is all zeroes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	for (i = 0; i < 2 * ps + nnote + nnote2; ++i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		if (buf[ph + i] != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 			goto nospace;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	/* fill in the program header entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	ns = ph + 2 * ps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	PUT_32(ph + PH_TYPE, PT_NOTE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	if (e_class == ELFCLASS32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		PUT_32(ph + PH_OFFSET, ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		PUT_64(ph + PH_OFFSET, ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	if (e_class == ELFCLASS32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		PUT_32(ph + PH_FILESZ, nnote);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		PUT_64(ph + PH_FILESZ, nnote);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	/* fill in the note area we point to */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	/* XXX we should probably make this a proper section */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	PUT_32(ns, strlen(arch) + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	PUT_32(ns + 4, N_DESCR * 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	PUT_32(ns + 8, 0x1275);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	strcpy((char *) &buf[ns + 12], arch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	ns += 12 + strlen(arch) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	for (i = 0; i < N_DESCR; ++i, ns += 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		PUT_32BE(ns, descr[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	/* fill in the second program header entry and the RPA note area */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	ph += ps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	PUT_32(ph + PH_TYPE, PT_NOTE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	if (e_class == ELFCLASS32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		PUT_32(ph + PH_OFFSET, ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		PUT_64(ph + PH_OFFSET, ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	if (e_class == ELFCLASS32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		PUT_32(ph + PH_FILESZ, nnote);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		PUT_64(ph + PH_FILESZ, nnote2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	/* fill in the note area we point to */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	PUT_32(ns, strlen(rpaname) + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	PUT_32(ns + 4, sizeof(rpanote));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	PUT_32(ns + 8, 0x12759999);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	strcpy((char *) &buf[ns + 12], rpaname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	ns += 12 + ROUNDUP(strlen(rpaname) + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	for (i = 0; i < N_RPA_DESCR; ++i, ns += 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		PUT_32BE(ns, rpanote[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	/* Update the number of program headers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	PUT_16(E_PHNUM, np + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	/* write back */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	i = lseek(fd, (long) 0, SEEK_SET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	if (i < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		perror("lseek");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	i = write(fd, buf, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	if (i < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		perror("write");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	if (i < n) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		fprintf(stderr, "%s: write truncated\n", av[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	exit(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)  notelf:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	fprintf(stderr, "%s does not appear to be an ELF file\n", av[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)  nospace:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	fprintf(stderr, "sorry, I can't find space in %s to put the note\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		av[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) }