Lines 43-48
Link Here
|
43 |
char *base_dir = NULL; |
43 |
char *base_dir = NULL; |
44 |
char *dest_dir = NULL; |
44 |
char *dest_dir = NULL; |
45 |
char *list_file = NULL; |
45 |
char *list_file = NULL; |
|
|
46 |
int print_files; |
47 |
int read_only; |
46 |
int list_file_fd = -1; |
48 |
int list_file_fd = -1; |
47 |
|
49 |
|
48 |
typedef unsigned int uint_32; |
50 |
typedef unsigned int uint_32; |
Lines 567-579
Link Here
|
567 |
char *p; |
569 |
char *p; |
568 |
size_t size; |
570 |
size_t size; |
569 |
ssize_t ret; |
571 |
ssize_t ret; |
570 |
if (base_dir) |
|
|
571 |
p = s + strlen (base_dir); |
572 |
else |
573 |
p = s; |
574 |
|
572 |
|
575 |
if (list_file_fd != -1) |
573 |
if (list_file_fd != -1) |
576 |
{ |
574 |
{ |
|
|
575 |
if (base_dir) |
576 |
p = s + strlen (base_dir); |
577 |
else |
578 |
p = s; |
577 |
size = strlen (p) + 1; |
579 |
size = strlen (p) + 1; |
578 |
while (size > 0) |
580 |
while (size > 0) |
579 |
{ |
581 |
{ |
Lines 584-589
Link Here
|
584 |
p += ret; |
586 |
p += ret; |
585 |
} |
587 |
} |
586 |
} |
588 |
} |
|
|
589 |
if (print_files) |
590 |
{ |
591 |
if (base_dir) |
592 |
p = s + strlen (base_dir); |
593 |
else |
594 |
p = s; |
595 |
size = strlen (p) + 1; |
596 |
while (size > 0) |
597 |
{ |
598 |
ret = write (STDOUT_FILENO, p, size); |
599 |
if (ret == -1) |
600 |
break; |
601 |
write (STDOUT_FILENO, "\n", 1); |
602 |
size -= ret; |
603 |
p += ret; |
604 |
} |
605 |
} |
587 |
} |
606 |
} |
588 |
|
607 |
|
589 |
free (s); |
608 |
free (s); |
Lines 1193-1198
Link Here
|
1193 |
"directory to rewrite base-dir into", NULL }, |
1212 |
"directory to rewrite base-dir into", NULL }, |
1194 |
{ "list-file", 'l', POPT_ARG_STRING, &list_file, 0, |
1213 |
{ "list-file", 'l', POPT_ARG_STRING, &list_file, 0, |
1195 |
"file where to put list of source and header file names", NULL }, |
1214 |
"file where to put list of source and header file names", NULL }, |
|
|
1215 |
{ "print", 'p', POPT_ARG_NONE, &print_files, 0, |
1216 |
"print newline delimited list of file names to stdout", NULL }, |
1217 |
{ "read-only", 'r', POPT_ARG_NONE, &read_only, 0, |
1218 |
"open read-only", NULL }, |
1196 |
POPT_AUTOHELP |
1219 |
POPT_AUTOHELP |
1197 |
{ NULL, 0, 0, NULL, 0, NULL, NULL } |
1220 |
{ NULL, 0, 0, NULL, 0, NULL, NULL } |
1198 |
}; |
1221 |
}; |
Lines 1205-1211
Link Here
|
1205 |
int i; |
1228 |
int i; |
1206 |
DSO *dso = NULL; |
1229 |
DSO *dso = NULL; |
1207 |
|
1230 |
|
1208 |
elf = elf_begin (fd, ELF_C_RDWR_MMAP, NULL); |
1231 |
elf = elf_begin (fd, read_only ? ELF_C_READ : ELF_C_RDWR_MMAP, NULL); |
1209 |
if (elf == NULL) |
1232 |
if (elf == NULL) |
1210 |
{ |
1233 |
{ |
1211 |
error (0, 0, "cannot open ELF file: %s", elf_errmsg (-1)); |
1234 |
error (0, 0, "cannot open ELF file: %s", elf_errmsg (-1)); |
Lines 1271-1287
Link Here
|
1271 |
return NULL; |
1294 |
return NULL; |
1272 |
} |
1295 |
} |
1273 |
|
1296 |
|
|
|
1297 |
static int process(const char *file) |
1298 |
{ |
1299 |
DSO *dso; |
1300 |
int fd, i; |
1301 |
struct stat stat_buf; |
1302 |
|
1303 |
if (stat(file, &stat_buf) < 0) |
1304 |
{ |
1305 |
fprintf (stderr, "Failed to open input file '%s': %s\n", file, strerror(errno)); |
1306 |
return -1; |
1307 |
} |
1308 |
|
1309 |
if (!read_only) { |
1310 |
/* Make sure we can read and write */ |
1311 |
chmod (file, stat_buf.st_mode | S_IRUSR | S_IWUSR); |
1312 |
} |
1313 |
|
1314 |
fd = open (file, read_only ? O_RDONLY : O_RDWR); |
1315 |
if (fd < 0) |
1316 |
{ |
1317 |
fprintf (stderr, "Failed to open input file '%s': %s\n", file, strerror(errno)); |
1318 |
return -1; |
1319 |
} |
1320 |
|
1321 |
dso = fdopen_dso (fd, file); |
1322 |
if (dso == NULL) |
1323 |
goto error; |
1324 |
|
1325 |
for (i = 1; i < dso->ehdr.e_shnum; i++) |
1326 |
{ |
1327 |
const char *name; |
1328 |
|
1329 |
switch (dso->shdr[i].sh_type) |
1330 |
{ |
1331 |
case SHT_PROGBITS: |
1332 |
name = strptr (dso, dso->ehdr.e_shstrndx, dso->shdr[i].sh_name); |
1333 |
/* TODO: Handle stabs */ |
1334 |
#if 0 |
1335 |
if (strcmp (name, ".stab") == 0) |
1336 |
edit_stabs (dso, i); |
1337 |
#endif |
1338 |
if (strcmp (name, ".debug_info") == 0) |
1339 |
edit_dwarf2 (dso); |
1340 |
|
1341 |
break; |
1342 |
default: |
1343 |
break; |
1344 |
} |
1345 |
} |
1346 |
|
1347 |
if (!read_only && elf_update (dso->elf, ELF_C_WRITE) < 0) |
1348 |
{ |
1349 |
fprintf (stderr, "Failed to write file: %s\n", elf_errmsg (elf_errno())); |
1350 |
goto error; |
1351 |
} |
1352 |
if (elf_end (dso->elf) < 0) |
1353 |
{ |
1354 |
fprintf (stderr, "elf_end failed: %s\n", elf_errmsg (elf_errno())); |
1355 |
goto error; |
1356 |
} |
1357 |
close (fd); |
1358 |
|
1359 |
if (!read_only) { |
1360 |
/* Restore old access rights */ |
1361 |
chmod (file, stat_buf.st_mode); |
1362 |
} |
1363 |
|
1364 |
return 0; |
1365 |
|
1366 |
error: |
1367 |
close(fd); |
1368 |
return -1; |
1369 |
} |
1274 |
|
1370 |
|
1275 |
int |
1371 |
int |
1276 |
main (int argc, char *argv[]) |
1372 |
main (int argc, char *argv[]) |
1277 |
{ |
1373 |
{ |
1278 |
DSO *dso; |
|
|
1279 |
int fd, i; |
1280 |
const char *file; |
1281 |
poptContext optCon; /* context for parsing command-line options */ |
1374 |
poptContext optCon; /* context for parsing command-line options */ |
1282 |
int nextopt; |
1375 |
int nextopt; |
1283 |
const char **args; |
1376 |
const char **args; |
1284 |
struct stat stat_buf; |
|
|
1285 |
char *p; |
1377 |
char *p; |
1286 |
|
1378 |
|
1287 |
optCon = poptGetContext("debugedit", argc, (const char **)argv, |
1379 |
optCon = poptGetContext("debugedit", argc, (const char **)argv, |
Lines 1300-1311
Link Here
|
1300 |
} |
1392 |
} |
1301 |
|
1393 |
|
1302 |
args = poptGetArgs (optCon); |
1394 |
args = poptGetArgs (optCon); |
1303 |
if (args == NULL || args[0] == NULL || args[1] != NULL) |
1395 |
if (args == NULL || args[0] == NULL) |
1304 |
{ |
1396 |
{ |
1305 |
poptPrintHelp(optCon, stdout, 0); |
1397 |
poptPrintHelp(optCon, stdout, 0); |
1306 |
exit (1); |
1398 |
exit (1); |
1307 |
} |
1399 |
} |
1308 |
|
1400 |
|
|
|
1401 |
if (elf_version(EV_CURRENT) == EV_NONE) |
1402 |
{ |
1403 |
fprintf (stderr, "library out of date\n"); |
1404 |
exit (1); |
1405 |
} |
1309 |
if (dest_dir != NULL) |
1406 |
if (dest_dir != NULL) |
1310 |
{ |
1407 |
{ |
1311 |
if (base_dir == NULL) |
1408 |
if (base_dir == NULL) |
Lines 1342-1413
Link Here
|
1342 |
{ |
1439 |
{ |
1343 |
list_file_fd = open (list_file, O_WRONLY|O_CREAT|O_APPEND, 0644); |
1440 |
list_file_fd = open (list_file, O_WRONLY|O_CREAT|O_APPEND, 0644); |
1344 |
} |
1441 |
} |
1345 |
|
|
|
1346 |
file = args[0]; |
1347 |
|
1442 |
|
1348 |
if (elf_version(EV_CURRENT) == EV_NONE) |
1443 |
while (*args) |
1349 |
{ |
1444 |
process(*args++); |
1350 |
fprintf (stderr, "library out of date\n"); |
|
|
1351 |
exit (1); |
1352 |
} |
1353 |
|
1445 |
|
1354 |
if (stat(file, &stat_buf) < 0) |
|
|
1355 |
{ |
1356 |
fprintf (stderr, "Failed to open input file '%s': %s\n", file, strerror(errno)); |
1357 |
exit (1); |
1358 |
} |
1359 |
|
1360 |
/* Make sure we can read and write */ |
1361 |
chmod (file, stat_buf.st_mode | S_IRUSR | S_IWUSR); |
1362 |
|
1363 |
fd = open (file, O_RDWR); |
1364 |
if (fd < 0) |
1365 |
{ |
1366 |
fprintf (stderr, "Failed to open input file '%s': %s\n", file, strerror(errno)); |
1367 |
exit (1); |
1368 |
} |
1369 |
|
1370 |
dso = fdopen_dso (fd, file); |
1371 |
if (dso == NULL) |
1372 |
exit (1); |
1373 |
|
1374 |
for (i = 1; i < dso->ehdr.e_shnum; i++) |
1375 |
{ |
1376 |
const char *name; |
1377 |
|
1378 |
switch (dso->shdr[i].sh_type) |
1379 |
{ |
1380 |
case SHT_PROGBITS: |
1381 |
name = strptr (dso, dso->ehdr.e_shstrndx, dso->shdr[i].sh_name); |
1382 |
/* TODO: Handle stabs */ |
1383 |
#if 0 |
1384 |
if (strcmp (name, ".stab") == 0) |
1385 |
edit_stabs (dso, i); |
1386 |
#endif |
1387 |
if (strcmp (name, ".debug_info") == 0) |
1388 |
edit_dwarf2 (dso); |
1389 |
|
1390 |
break; |
1391 |
default: |
1392 |
break; |
1393 |
} |
1394 |
} |
1395 |
|
1396 |
if (elf_update (dso->elf, ELF_C_WRITE) < 0) |
1397 |
{ |
1398 |
fprintf (stderr, "Failed to write file: %s\n", elf_errmsg (elf_errno())); |
1399 |
exit (1); |
1400 |
} |
1401 |
if (elf_end (dso->elf) < 0) |
1402 |
{ |
1403 |
fprintf (stderr, "elf_end failed: %s\n", elf_errmsg (elf_errno())); |
1404 |
exit (1); |
1405 |
} |
1406 |
close (fd); |
1407 |
|
1408 |
/* Restore old access rights */ |
1409 |
chmod (file, stat_buf.st_mode); |
1410 |
|
1411 |
poptFreeContext (optCon); |
1446 |
poptFreeContext (optCon); |
1412 |
|
1447 |
|
1413 |
return 0; |
1448 |
return 0; |