Deleted Added
full compact
segments.c (292120) segments.c (295577)
1/*-
2 * Copyright (c) 2007-2010,2012 Kai Wang
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 20 unchanged lines hidden (view full) ---

29#include <gelf.h>
30#include <stdint.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34
35#include "elfcopy.h"
36
1/*-
2 * Copyright (c) 2007-2010,2012 Kai Wang
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 20 unchanged lines hidden (view full) ---

29#include <gelf.h>
30#include <stdint.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34
35#include "elfcopy.h"
36
37ELFTC_VCSID("$Id: segments.c 3269 2015-12-11 18:38:43Z kaiwang27 $");
37ELFTC_VCSID("$Id: segments.c 3397 2016-02-12 14:35:19Z emaste $");
38
39static void insert_to_inseg_list(struct segment *seg, struct section *sec);
40
41/*
42 * elfcopy's segment handling is relatively simpler and less powerful than
43 * libbfd. Program headers are modified or copied from input to output objects,
44 * but never re-generated. As a result, if the input object has incorrect
45 * program headers, the output object's program headers will remain incorrect

--- 56 unchanged lines hidden (view full) ---

102 int found, i;
103
104 /*
105 * Apply VMA and global LMA changes in the first iteration.
106 */
107 TAILQ_FOREACH(s, &ecp->v_sec, sec_list) {
108
109 /* Only adjust loadable section's address. */
38
39static void insert_to_inseg_list(struct segment *seg, struct section *sec);
40
41/*
42 * elfcopy's segment handling is relatively simpler and less powerful than
43 * libbfd. Program headers are modified or copied from input to output objects,
44 * but never re-generated. As a result, if the input object has incorrect
45 * program headers, the output object's program headers will remain incorrect

--- 56 unchanged lines hidden (view full) ---

102 int found, i;
103
104 /*
105 * Apply VMA and global LMA changes in the first iteration.
106 */
107 TAILQ_FOREACH(s, &ecp->v_sec, sec_list) {
108
109 /* Only adjust loadable section's address. */
110 if (!s->loadable || s->seg == NULL)
110 if (!s->loadable)
111 continue;
112
113 /* Apply global LMA adjustment. */
111 continue;
112
113 /* Apply global LMA adjustment. */
114 if (ecp->change_addr != 0)
114 if (ecp->change_addr != 0 && s->seg != NULL)
115 s->lma += ecp->change_addr;
116
117 if (!s->pseudo) {
118 /* Apply global VMA adjustment. */
119 if (ecp->change_addr != 0)
120 s->vma += ecp->change_addr;
121
122 /* Apply section VMA adjustment. */

--- 7 unchanged lines hidden (view full) ---

130 }
131 }
132
133 /*
134 * Apply sections LMA change in the second iteration.
135 */
136 TAILQ_FOREACH(s, &ecp->v_sec, sec_list) {
137
115 s->lma += ecp->change_addr;
116
117 if (!s->pseudo) {
118 /* Apply global VMA adjustment. */
119 if (ecp->change_addr != 0)
120 s->vma += ecp->change_addr;
121
122 /* Apply section VMA adjustment. */

--- 7 unchanged lines hidden (view full) ---

130 }
131 }
132
133 /*
134 * Apply sections LMA change in the second iteration.
135 */
136 TAILQ_FOREACH(s, &ecp->v_sec, sec_list) {
137
138 /* Only adjust loadable section's LMA. */
138 /*
139 * Only loadable section that's inside a segment can have
140 * LMA adjusted.
141 */
139 if (!s->loadable || s->seg == NULL)
140 continue;
141
142 /*
143 * Check if there is a LMA change request for this
144 * section.
145 */
146 sac = lookup_sec_act(ecp, s->name, 0);

--- 21 unchanged lines hidden (view full) ---

168 */
169#ifdef DEBUG
170 printf("LMA for section %s: %#jx\n", s->name, lma);
171#endif
172
173 if (lma % s->align != 0)
174 errx(EXIT_FAILURE, "The load address %#jx for "
175 "section %s is not aligned to %ju",
142 if (!s->loadable || s->seg == NULL)
143 continue;
144
145 /*
146 * Check if there is a LMA change request for this
147 * section.
148 */
149 sac = lookup_sec_act(ecp, s->name, 0);

--- 21 unchanged lines hidden (view full) ---

171 */
172#ifdef DEBUG
173 printf("LMA for section %s: %#jx\n", s->name, lma);
174#endif
175
176 if (lma % s->align != 0)
177 errx(EXIT_FAILURE, "The load address %#jx for "
178 "section %s is not aligned to %ju",
176 (uintmax_t) lma, s->name, s->align);
179 (uintmax_t) lma, s->name, (uintmax_t) s->align);
177
178 if (lma < s->lma) {
179 /* Move section to lower address. */
180 if (lma < s->lma - s->seg->addr)
181 errx(EXIT_FAILURE, "Not enough space to move "
182 "section %s load address to %#jx", s->name,
183 (uintmax_t) lma);
184 start = lma - (s->lma - s->seg->addr);

--- 24 unchanged lines hidden (view full) ---

209 if (seg == s->seg || seg->type != PT_LOAD)
210 continue;
211 if (start > seg->addr + seg->msz)
212 continue;
213 if (end < seg->addr)
214 continue;
215 errx(EXIT_FAILURE, "The extent of segment containing "
216 "section %s overlaps with segment(%#jx,%#jx)",
180
181 if (lma < s->lma) {
182 /* Move section to lower address. */
183 if (lma < s->lma - s->seg->addr)
184 errx(EXIT_FAILURE, "Not enough space to move "
185 "section %s load address to %#jx", s->name,
186 (uintmax_t) lma);
187 start = lma - (s->lma - s->seg->addr);

--- 24 unchanged lines hidden (view full) ---

212 if (seg == s->seg || seg->type != PT_LOAD)
213 continue;
214 if (start > seg->addr + seg->msz)
215 continue;
216 if (end < seg->addr)
217 continue;
218 errx(EXIT_FAILURE, "The extent of segment containing "
219 "section %s overlaps with segment(%#jx,%#jx)",
217 s->name, seg->addr, seg->addr + seg->msz);
220 s->name, (uintmax_t) seg->addr,
221 (uintmax_t) (seg->addr + seg->msz));
218 }
219
220 /*
221 * Update section LMA and file offset.
222 */
223
224 if (lma < s->lma) {
225 /*

--- 254 unchanged lines hidden (view full) ---

480 ophdr.p_vaddr = seg->addr;
481 ophdr.p_paddr = seg->addr;
482 ophdr.p_flags = iphdr.p_flags;
483 ophdr.p_align = iphdr.p_align;
484 ophdr.p_offset = seg->off;
485 ophdr.p_filesz = seg->fsz;
486 ophdr.p_memsz = seg->msz;
487 if (!gelf_update_phdr(ecp->eout, i, &ophdr))
222 }
223
224 /*
225 * Update section LMA and file offset.
226 */
227
228 if (lma < s->lma) {
229 /*

--- 254 unchanged lines hidden (view full) ---

484 ophdr.p_vaddr = seg->addr;
485 ophdr.p_paddr = seg->addr;
486 ophdr.p_flags = iphdr.p_flags;
487 ophdr.p_align = iphdr.p_align;
488 ophdr.p_offset = seg->off;
489 ophdr.p_filesz = seg->fsz;
490 ophdr.p_memsz = seg->msz;
491 if (!gelf_update_phdr(ecp->eout, i, &ophdr))
488 err(EXIT_FAILURE, "gelf_update_phdr failed :%s",
492 errx(EXIT_FAILURE, "gelf_update_phdr failed: %s",
489 elf_errmsg(-1));
490
491 i++;
492 }
493}
493 elf_errmsg(-1));
494
495 i++;
496 }
497}