4
4
5
5
#include <errno.h>
6
6
#include <unistd.h>
7
- #include <stdio.h>
8
7
#include <fcntl.h>
9
8
#include <string.h>
10
9
#include <stdlib.h>
@@ -88,11 +87,8 @@ int filename__read_debuglink(const char *filename __maybe_unused,
88
87
*/
89
88
int filename__read_build_id (const char * filename , struct build_id * bid )
90
89
{
91
- FILE * fp ;
92
- int ret = -1 ;
90
+ int fd , ret = -1 ;
93
91
bool need_swap = false, elf32 ;
94
- u8 e_ident [EI_NIDENT ];
95
- int i ;
96
92
union {
97
93
struct {
98
94
Elf32_Ehdr ehdr32 ;
@@ -103,28 +99,27 @@ int filename__read_build_id(const char *filename, struct build_id *bid)
103
99
Elf64_Phdr * phdr64 ;
104
100
};
105
101
} hdrs ;
106
- void * phdr ;
107
- size_t phdr_size ;
108
- void * buf = NULL ;
109
- size_t buf_size = 0 ;
102
+ void * phdr , * buf = NULL ;
103
+ ssize_t phdr_size , ehdr_size , buf_size = 0 ;
110
104
111
- fp = fopen (filename , "r" );
112
- if (fp == NULL )
105
+ fd = open (filename , O_RDONLY );
106
+ if (fd < 0 )
113
107
return -1 ;
114
108
115
- if (fread ( e_ident , sizeof ( e_ident ), 1 , fp ) != 1 )
109
+ if (read ( fd , hdrs . ehdr32 . e_ident , EI_NIDENT ) != EI_NIDENT )
116
110
goto out ;
117
111
118
- if (memcmp (e_ident , ELFMAG , SELFMAG ) ||
119
- e_ident [EI_VERSION ] != EV_CURRENT )
112
+ if (memcmp (hdrs . ehdr32 . e_ident , ELFMAG , SELFMAG ) ||
113
+ hdrs . ehdr32 . e_ident [EI_VERSION ] != EV_CURRENT )
120
114
goto out ;
121
115
122
- need_swap = check_need_swap (e_ident [EI_DATA ]);
123
- elf32 = e_ident [EI_CLASS ] == ELFCLASS32 ;
116
+ need_swap = check_need_swap (hdrs .ehdr32 .e_ident [EI_DATA ]);
117
+ elf32 = hdrs .ehdr32 .e_ident [EI_CLASS ] == ELFCLASS32 ;
118
+ ehdr_size = (elf32 ? sizeof (hdrs .ehdr32 ) : sizeof (hdrs .ehdr64 )) - EI_NIDENT ;
124
119
125
- if (fread ( elf32 ? ( void * ) & hdrs . ehdr32 : ( void * ) & hdrs . ehdr64 ,
126
- elf32 ? sizeof ( hdrs .ehdr32 ) : sizeof ( hdrs .ehdr64 ),
127
- 1 , fp ) != 1 )
120
+ if (read ( fd ,
121
+ ( elf32 ? ( void * ) & hdrs .ehdr32 : ( void * ) & hdrs .ehdr64 ) + EI_NIDENT ,
122
+ ehdr_size ) != ehdr_size )
128
123
goto out ;
129
124
130
125
if (need_swap ) {
@@ -138,23 +133,27 @@ int filename__read_build_id(const char *filename, struct build_id *bid)
138
133
hdrs .ehdr64 .e_phnum = bswap_16 (hdrs .ehdr64 .e_phnum );
139
134
}
140
135
}
141
- phdr_size = elf32 ? hdrs .ehdr32 .e_phentsize * hdrs .ehdr32 .e_phnum
142
- : hdrs .ehdr64 .e_phentsize * hdrs .ehdr64 .e_phnum ;
136
+ if ((elf32 && hdrs .ehdr32 .e_phentsize != sizeof (Elf32_Phdr )) ||
137
+ (!elf32 && hdrs .ehdr64 .e_phentsize != sizeof (Elf64_Phdr )))
138
+ goto out ;
139
+
140
+ phdr_size = elf32 ? sizeof (Elf32_Phdr ) * hdrs .ehdr32 .e_phnum
141
+ : sizeof (Elf64_Phdr ) * hdrs .ehdr64 .e_phnum ;
143
142
phdr = malloc (phdr_size );
144
143
if (phdr == NULL )
145
144
goto out ;
146
145
147
- fseek ( fp , elf32 ? hdrs .ehdr32 .e_phoff : hdrs .ehdr64 .e_phoff , SEEK_SET );
148
- if (fread ( phdr , phdr_size , 1 , fp ) != 1 )
146
+ lseek ( fd , elf32 ? hdrs .ehdr32 .e_phoff : hdrs .ehdr64 .e_phoff , SEEK_SET );
147
+ if (read ( fd , phdr , phdr_size ) != phdr_size )
149
148
goto out_free ;
150
149
151
150
if (elf32 )
152
151
hdrs .phdr32 = phdr ;
153
152
else
154
153
hdrs .phdr64 = phdr ;
155
154
156
- for (i = 0 ; i < elf32 ? hdrs .ehdr32 .e_phnum : hdrs .ehdr64 .e_phnum ; i ++ ) {
157
- size_t p_filesz ;
155
+ for (int i = 0 ; i < ( elf32 ? hdrs .ehdr32 .e_phnum : hdrs .ehdr64 .e_phnum ) ; i ++ ) {
156
+ ssize_t p_filesz ;
158
157
159
158
if (need_swap ) {
160
159
if (elf32 ) {
@@ -180,8 +179,8 @@ int filename__read_build_id(const char *filename, struct build_id *bid)
180
179
goto out_free ;
181
180
buf = tmp ;
182
181
}
183
- fseek ( fp , elf32 ? hdrs .phdr32 [i ].p_offset : hdrs .phdr64 [i ].p_offset , SEEK_SET );
184
- if (fread ( buf , p_filesz , 1 , fp ) != 1 )
182
+ lseek ( fd , elf32 ? hdrs .phdr32 [i ].p_offset : hdrs .phdr64 [i ].p_offset , SEEK_SET );
183
+ if (read ( fd , buf , p_filesz ) != p_filesz )
185
184
goto out_free ;
186
185
187
186
ret = read_build_id (buf , p_filesz , bid , need_swap );
@@ -194,7 +193,7 @@ int filename__read_build_id(const char *filename, struct build_id *bid)
194
193
free (buf );
195
194
free (phdr );
196
195
out :
197
- fclose ( fp );
196
+ close ( fd );
198
197
return ret ;
199
198
}
200
199
0 commit comments