-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjcopy.c
More file actions
58 lines (46 loc) · 1.07 KB
/
Copy pathobjcopy.c
File metadata and controls
58 lines (46 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//objcopy
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <bfd.h>
#include <sys/stat.h>
#include <fcntl.h>
static bfd_boolean sect_objcopy_write(bfd *abfd, asection *asec, void *arg)
{
int out_fd = *(int*)arg;
bfd_byte *sect_data;
unsigned long sect_size = bfd_section_size(abfd, asec);
if (bfd_malloc_and_get_section(abfd, asec, §_data) == TRUE)
{
write(out_fd, sect_data, sect_size);
free(sect_data);
}
return FALSE; //keep looking for next match
}
int sect_objcopy (const char* infile, const char* match_str, const char* outfile)
{
int out_fd = -1;
bfd *abfd;
bfd_init();
abfd = bfd_openr (infile, "elf64-x86-64");
if (abfd == NULL)
{
bfd_perror("bfd_openr error");
return -1;
}
if (bfd_check_format(abfd, bfd_object) != TRUE)
{
bfd_close(abfd);
return -1;
}
out_fd = open(outfile, O_CREAT | O_WRONLY);
if (out_fd == -1)
{
bfd_close(abfd);
return -1;
}
bfd_get_section_by_name_if(abfd, match_str, sect_objcopy_write, &out_fd);
close(out_fd);
bfd_close(abfd);
return 0;
}