summaryrefslogtreecommitdiffstats
path: root/rtemstoolkit/elftoolchain/libelf/libelf_ar_util.c
diff options
context:
space:
mode:
Diffstat (limited to 'rtemstoolkit/elftoolchain/libelf/libelf_ar_util.c')
-rw-r--r--rtemstoolkit/elftoolchain/libelf/libelf_ar_util.c64
1 files changed, 35 insertions, 29 deletions
diff --git a/rtemstoolkit/elftoolchain/libelf/libelf_ar_util.c b/rtemstoolkit/elftoolchain/libelf/libelf_ar_util.c
index 7051fe8..239612e 100644
--- a/rtemstoolkit/elftoolchain/libelf/libelf_ar_util.c
+++ b/rtemstoolkit/elftoolchain/libelf/libelf_ar_util.c
@@ -24,8 +24,6 @@
* SUCH DAMAGE.
*/
-#include <sys/cdefs.h>
-
#include <assert.h>
#include <libelf.h>
#include <stdlib.h>
@@ -34,21 +32,23 @@
#include "_libelf.h"
#include "_libelf_ar.h"
-LIBELF_VCSID("$Id: libelf_ar_util.c 2066 2011-10-26 15:40:28Z jkoshy $");
+ELFTC_VCSID("$Id: libelf_ar_util.c 3174 2015-03-27 17:13:41Z emaste $");
/*
* Convert a string bounded by `start' and `start+sz' (exclusive) to a
* number in the specified base.
*/
int
-_libelf_ar_get_number(const char *s, size_t sz, int base, size_t *ret)
+_libelf_ar_get_number(const char *src, size_t sz, unsigned int base,
+ size_t *ret)
{
- int c, v;
size_t r;
- const char *e;
+ unsigned int c, v;
+ const unsigned char *e, *s;
assert(base <= 10);
+ s = (const unsigned char *) src;
e = s + sz;
/* skip leading blanks */
@@ -79,17 +79,18 @@ _libelf_ar_get_number(const char *s, size_t sz, int base, size_t *ret)
char *
_libelf_ar_get_translated_name(const struct ar_hdr *arh, Elf *ar)
{
- char c, *s;
+ char *s;
+ unsigned char c;
size_t len, offset;
- const char *buf, *p, *q, *r;
+ const unsigned char *buf, *p, *q, *r;
const size_t bufsize = sizeof(arh->ar_name);
assert(arh != NULL);
assert(ar->e_kind == ELF_K_AR);
- assert((const char *) arh >= ar->e_rawfile &&
- (const char *) arh < ar->e_rawfile + ar->e_rawsize);
+ assert((const unsigned char *) arh >= ar->e_rawfile &&
+ (const unsigned char *) arh < ar->e_rawfile + ar->e_rawsize);
- buf = arh->ar_name;
+ buf = (const unsigned char *) arh->ar_name;
/*
* Check for extended naming.
@@ -104,8 +105,8 @@ _libelf_ar_get_translated_name(const struct ar_hdr *arh, Elf *ar)
* the archive string table where the actual name
* resides.
*/
- if (_libelf_ar_get_number(buf + 1, bufsize - 1, 10,
- &offset) == 0) {
+ if (_libelf_ar_get_number((const char *) (buf + 1),
+ bufsize - 1, 10, &offset) == 0) {
LIBELF_SET_ERROR(ARCHIVE, 0);
return (NULL);
}
@@ -120,21 +121,21 @@ _libelf_ar_get_translated_name(const struct ar_hdr *arh, Elf *ar)
for (; p < r && *p != '/'; p++)
;
- len = p - q + 1; /* space for the trailing NUL */
+ len = (size_t) (p - q + 1); /* space for the trailing NUL */
if ((s = malloc(len)) == NULL) {
LIBELF_SET_ERROR(RESOURCE, 0);
return (NULL);
}
- (void) strncpy(s, q, len - 1);
+ (void) strncpy(s, (const char *) q, len - 1);
s[len - 1] = '\0';
return (s);
} else if (IS_EXTENDED_BSD_NAME(buf)) {
r = buf + LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE;
- if (_libelf_ar_get_number(r, bufsize -
+ if (_libelf_ar_get_number((const char *) r, bufsize -
LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE, 10,
&len) == 0) {
LIBELF_SET_ERROR(ARCHIVE, 0);
@@ -153,9 +154,9 @@ _libelf_ar_get_translated_name(const struct ar_hdr *arh, Elf *ar)
/*
* The file name follows the archive header.
*/
- q = (const char *) (arh + 1);
+ q = (const unsigned char *) (arh + 1);
- (void) strncpy(s, q, len);
+ (void) strncpy(s, (const char *) q, len);
s[len] = '\0';
return (s);
@@ -183,10 +184,10 @@ _libelf_ar_get_translated_name(const struct ar_hdr *arh, Elf *ar)
q--;
}
- len = q - buf + 2; /* Add space for a trailing NUL. */
+ len = (size_t) (q - buf + 2); /* Space for a trailing NUL. */
} else {
/* The buffer only had blanks. */
- buf = "";
+ buf = (const unsigned char *) "";
len = 1;
}
@@ -195,7 +196,7 @@ _libelf_ar_get_translated_name(const struct ar_hdr *arh, Elf *ar)
return (NULL);
}
- (void) strncpy(s, buf, len - 1);
+ (void) strncpy(s, (const char *) buf, len - 1);
s[len - 1] = '\0';
return (s);
@@ -225,14 +226,15 @@ _libelf_ar_get_raw_name(const struct ar_hdr *arh)
* Open an 'ar' archive.
*/
Elf *
-_libelf_ar_open(Elf *e)
+_libelf_ar_open(Elf *e, int reporterror)
{
- int scanahead;
- char *s, *end;
size_t sz;
+ int scanahead;
struct ar_hdr arh;
+ unsigned char *s, *end;
+
+ _libelf_init_elf(e, ELF_K_AR);
- e->e_kind = ELF_K_AR;
e->e_u.e_ar.e_nchildren = 0;
e->e_u.e_ar.e_next = (off_t) -1;
@@ -263,7 +265,7 @@ _libelf_ar_open(Elf *e)
(void) memcpy(&(ARH), (S), sizeof((ARH))); \
if ((ARH).ar_fmag[0] != '`' || (ARH).ar_fmag[1] != '\n') \
goto error; \
- if (_libelf_ar_get_number((ARH).ar_size, \
+ if (_libelf_ar_get_number((char *) (ARH).ar_size, \
sizeof((ARH).ar_size), 10, &(SZ)) == 0) \
goto error; \
} while (0)
@@ -274,8 +276,8 @@ _libelf_ar_open(Elf *e)
* Handle special archive members for the SVR4 format.
*/
if (arh.ar_name[0] == '/') {
-
- assert(sz > 0);
+ if (sz == 0)
+ goto error;
e->e_flags |= LIBELF_F_AR_VARIANT_SVR4;
@@ -348,7 +350,11 @@ _libelf_ar_open(Elf *e)
return (e);
error:
+ if (!reporterror) {
+ e->e_kind = ELF_K_NONE;
+ return (e);
+ }
+
LIBELF_SET_ERROR(ARCHIVE, 0);
return (NULL);
-
}