Blame src/arbits/slbt_archive_meta.c

d4473b
/*******************************************************************/
eac61a
/*  slibtool: a strong libtool implementation, written in C        */
d4473b
/*  Copyright (C) 2016--2024  SysDeer Technologies, LLC            */
d4473b
/*  Released under the Standard MIT License; see COPYING.SLIBTOOL. */
d4473b
/*******************************************************************/
d4473b
d4473b
#include <stdint.h>
d4473b
#include <stddef.h>
d4473b
#include <stdlib.h>
d4473b
#include <string.h>
d4473b
#include <sys/mman.h>
d4473b
d4473b
#include <slibtool/slibtool.h>
d4473b
#include <slibtool/slibtool_arbits.h>
d4473b
#include "slibtool_ar_impl.h"
d4473b
#include "slibtool_driver_impl.h"
d4473b
#include "slibtool_errinfo_impl.h"
4b56de
#include "slibtool_visibility_impl.h"
d4473b
d4473b
/* transient header info vector */
d4473b
struct ar_header_info {
d4473b
	struct ar_raw_file_header * phdr;
d4473b
	uint32_t                    attr;
d4473b
};
d4473b
d4473b
static const char ar_signature[] = AR_SIGNATURE;
d4473b
51c276
static int slbt_ar_free_archive_meta_impl(struct slbt_archive_meta_impl * meta, int ret)
d4473b
{
d4473b
	if (meta) {
7fe83d
		if (meta->armaps.armap_symrefs_32)
7fe83d
			free(meta->armaps.armap_symrefs_32);
7fe83d
7fe83d
		if (meta->armaps.armap_symrefs_64)
7fe83d
			free(meta->armaps.armap_symrefs_64);
7fe83d
d4473b
		if (meta->hdrinfov)
d4473b
			free(meta->hdrinfov);
d4473b
d4473b
		if (meta->namestrs)
d4473b
			free(meta->namestrs);
d4473b
bbf27c
		if (meta->syminfo)
bbf27c
			free(meta->syminfo);
bbf27c
bbf27c
		if (meta->syminfv)
bbf27c
			free(meta->syminfv);
bbf27c
d4473b
		if (meta->memberv)
d4473b
			free(meta->memberv);
d4473b
405946
		if (meta->offsetv)
405946
			free(meta->offsetv);
405946
d4473b
		if (meta->members)
d4473b
			free(meta->members);
d4473b
d4473b
		if (meta->symstrv)
d4473b
			free(meta->symstrv);
d4473b
df2627
		if (meta->mapstrv)
df2627
			free(meta->mapstrv);
df2627
bbf27c
		if (meta->nminfo)
bbf27c
			slbt_lib_free_txtfile_ctx(meta->nminfo);
bbf27c
d4473b
		free(meta);
d4473b
	}
d4473b
d4473b
	return ret;
d4473b
}
d4473b
d4473b
d4473b
static int slbt_ar_read_octal(const char * mark, int len, uint32_t * dec)
d4473b
{
d4473b
	int       i;
d4473b
	uint64_t  res;
d4473b
d4473b
	for (; len && (mark[len-1]==AR_DEC_PADDING); )
d4473b
		len--;
d4473b
d4473b
	for (i=0,res=0; i
d4473b
		if ((mark[i] >= '0') && (mark[i] <= '7')) {
d4473b
			res *= 8;
d4473b
			res += (mark[i] - '0');
d4473b
		} else {
d4473b
			return -1;
d4473b
		}
d4473b
	}
d4473b
d4473b
	*dec = res;
d4473b
d4473b
	return 0;
d4473b
}
d4473b
d4473b
static int slbt_ar_read_decimal_64(const char * mark, int len, uint64_t * dec)
d4473b
{
d4473b
	int       i;
d4473b
	uint64_t  res;
d4473b
d4473b
	for (; len && (mark[len-1]==AR_DEC_PADDING); )
d4473b
		len--;
d4473b
d4473b
	for (i=0,res=0; i
d4473b
		if ((mark[i] >= '0') && (mark[i] <= '9')) {
d4473b
			res *= 10;
d4473b
			res += (mark[i] - '0');
d4473b
		} else {
d4473b
			return -1;
d4473b
		}
d4473b
	}
d4473b
d4473b
	*dec = res;
d4473b
d4473b
	return 0;
d4473b
}
d4473b
d4473b
static int slbt_ar_read_decimal_32(const char * mark, int len, uint32_t * dec)
d4473b
{
d4473b
	uint64_t res;
d4473b
d4473b
	if (slbt_ar_read_decimal_64(mark,len,&res) < 0)
d4473b
		return -1;
d4473b
d4473b
	*dec = res;
d4473b
d4473b
	return 0;
d4473b
}
d4473b
d4473b
static uint32_t slbt_ar_get_member_attr(struct ar_meta_member_info * m)
d4473b
{
d4473b
	const char *            hdrname;
d4473b
	uint32_t                hdrattr;
d4473b
	const char *            data;
d4473b
	const char *            data_cap;
d4473b
	const unsigned char *   udata;
d4473b
	unsigned char           uch;
d4473b
	const size_t            siglen = sizeof(struct ar_raw_signature);
d4473b
d4473b
	hdrname  = m->ar_file_header.ar_member_name;
d4473b
	hdrattr  = m->ar_file_header.ar_header_attr;
d4473b
d4473b
	data     = m->ar_object_data;
d4473b
	data_cap = &data[m->ar_file_header.ar_file_size];
d4473b
d4473b
	if (hdrattr & AR_HEADER_ATTR_SYSV) {
d4473b
		/* long names member? */
d4473b
		if ((hdrname[0] == '/') && (hdrname[1] == '/'))
d4473b
			return AR_MEMBER_ATTR_NAMESTRS;
d4473b
d4473b
		/* mips 64-bit armap member? */
d4473b
		else if (!strncmp(hdrname,"/SYM64/",7))
d4473b
			return AR_MEMBER_ATTR_ARMAP;
d4473b
d4473b
		/* armap member? */
d4473b
		else if (hdrname[0] == '/' && (hdrname[1] == '\0'))
d4473b
			return AR_MEMBER_ATTR_ARMAP;
d4473b
d4473b
		/* nested archive? */
d4473b
		else if (m->ar_file_header.ar_file_size >= siglen)
d4473b
			if (!strncmp(data,ar_signature,siglen))
d4473b
				return AR_MEMBER_ATTR_ARCHIVE;
d4473b
d4473b
	} else if (hdrattr & AR_HEADER_ATTR_BSD) {
d4473b
		if (!strcmp(hdrname,"__.SYMDEF"))
d4473b
			return AR_MEMBER_ATTR_ARMAP;
d4473b
d4473b
		else if (!strcmp(hdrname,"__.SYMDEF SORTED"))
d4473b
			return AR_MEMBER_ATTR_ARMAP;
d4473b
d4473b
		else if (!strcmp(hdrname,"__.SYMDEF_64"))
d4473b
			return AR_MEMBER_ATTR_ARMAP;
d4473b
d4473b
		else if (!strcmp(hdrname,"__.SYMDEF_64 SORTED"))
d4473b
			return AR_MEMBER_ATTR_ARMAP;
d4473b
	}
d4473b
d4473b
	/* ascii only data? */
d4473b
	for (; data
d4473b
		if ((uch = *data) >= 0x80)
d4473b
			break;
d4473b
d4473b
		data++;
d4473b
	}
d4473b
d4473b
	if (data == data_cap)
d4473b
		return AR_MEMBER_ATTR_ASCII;
d4473b
d4473b
	data  = m->ar_object_data;
d4473b
	udata = (unsigned char *)data;
d4473b
d4473b
	/* elf object? [quick and dirty] */
d4473b
	if (m->ar_file_header.ar_file_size >= 5)
d4473b
		if ((udata[0] == 0x7f)
d4473b
				&& (udata[1] == 'E')
d4473b
				&& (udata[2] == 'L')
d4473b
				&& (udata[3] == 'F'))
d4473b
			if ((m->ar_object_attr = AR_OBJECT_ATTR_ELF))
d4473b
				return AR_MEMBER_ATTR_OBJECT;
d4473b
d4473b
	/* coff i386 object? [quick and dirty] */
d4473b
	if (m->ar_file_header.ar_file_size >= 2)
d4473b
		if ((udata[0] == 0x4c) && (udata[1] == 0x01))
d4473b
			if ((m->ar_object_attr = AR_OBJECT_ATTR_COFF))
d4473b
				return AR_MEMBER_ATTR_OBJECT;
d4473b
d4473b
	/* coff x86_64 object? [quick and dirty] */
d4473b
	if (m->ar_file_header.ar_file_size >= 2)
d4473b
		if ((udata[0] == 0x64) && (udata[1] == 0x86))
d4473b
			if ((m->ar_object_attr = AR_OBJECT_ATTR_COFF))
d4473b
				return AR_MEMBER_ATTR_OBJECT;
d4473b
d4473b
	/* big endian 32-bit macho object? [quick and dirty] */
d4473b
	if (m->ar_file_header.ar_file_size >= 4)
d4473b
		if ((udata[0] == 0xfe) && (udata[1] == 0xed))
d4473b
			if ((udata[2] == 0xfa) && (udata[3] == 0xce))
d4473b
				if ((m->ar_object_attr = AR_OBJECT_ATTR_MACHO))
d4473b
					return AR_MEMBER_ATTR_OBJECT;
d4473b
d4473b
	/* big endian 64-bit macho object? [quick and dirty] */
d4473b
	if (m->ar_file_header.ar_file_size >= 4)
d4473b
		if ((udata[0] == 0xfe) && (udata[1] == 0xed))
d4473b
			if ((udata[2] == 0xfa) && (udata[3] == 0xcf))
d4473b
				if ((m->ar_object_attr = AR_OBJECT_ATTR_MACHO))
d4473b
					return AR_MEMBER_ATTR_OBJECT;
d4473b
d4473b
	/* little endian 32-bit macho object? [quick and dirty] */
d4473b
	if (m->ar_file_header.ar_file_size >= 4)
d4473b
		if ((udata[3] == 0xfe) && (udata[2] == 0xed))
d4473b
			if ((udata[1] == 0xfa) && (udata[0] == 0xce))
d4473b
				if ((m->ar_object_attr = AR_OBJECT_ATTR_MACHO))
d4473b
					return AR_MEMBER_ATTR_OBJECT;
d4473b
d4473b
	/* little endian 64-bit macho object? [quick and dirty] */
d4473b
	if (m->ar_file_header.ar_file_size >= 4)
d4473b
		if ((udata[3] == 0xfe) && (udata[2] == 0xed))
d4473b
			if ((udata[1] == 0xfa) && (udata[0] == 0xcf))
d4473b
				if ((m->ar_object_attr = AR_OBJECT_ATTR_MACHO))
d4473b
					return AR_MEMBER_ATTR_OBJECT;
d4473b
d4473b
	/* all other */
d4473b
	return AR_MEMBER_ATTR_DEFAULT;
d4473b
}
d4473b
d4473b
static int slbt_ar_parse_primary_armap(
d4473b
	const struct slbt_driver_ctx *  dctx,
d4473b
	struct slbt_archive_meta_impl * m)
d4473b
d4473b
{
d4473b
	struct ar_meta_member_info *    memberp;
d4473b
	const char *                    hdrname;
d4473b
	uint32_t                        hdrattr;
d4473b
d4473b
	memberp = m->memberv[0];
d4473b
	hdrname = memberp->ar_file_header.ar_member_name;
d4473b
	hdrattr = memberp->ar_file_header.ar_header_attr;
d4473b
d4473b
	if (!(memberp->ar_member_attr & AR_MEMBER_ATTR_ARMAP))
d4473b
		return 0;
d4473b
d4473b
	if (hdrattr & AR_HEADER_ATTR_SYSV) {
d4473b
		/* mips 64-bit armap member? */
d4473b
		if (!strncmp(hdrname,"/SYM64/",7))
d4473b
			return slbt_ar_parse_primary_armap_sysv_64(
d4473b
				dctx,m);
d4473b
d4473b
		/* sysv 32-bit armap member */
d4473b
		return slbt_ar_parse_primary_armap_sysv_32(
d4473b
			dctx,m);
d4473b
d4473b
	} else if (hdrattr & AR_HEADER_ATTR_BSD) {
d4473b
		if (!strcmp(hdrname,"__.SYMDEF"))
d4473b
			return slbt_ar_parse_primary_armap_bsd_32(
d4473b
				dctx,m);
d4473b
d4473b
		else if (!strcmp(hdrname,"__.SYMDEF SORTED"))
d4473b
			return slbt_ar_parse_primary_armap_bsd_32(
d4473b
				dctx,m);
d4473b
d4473b
		else if (!strcmp(hdrname,"__.SYMDEF_64"))
d4473b
			return slbt_ar_parse_primary_armap_bsd_64(
d4473b
				dctx,m);
d4473b
d4473b
		else if (!strcmp(hdrname,"__.SYMDEF_64 SORTED"))
d4473b
			return slbt_ar_parse_primary_armap_bsd_64(
d4473b
				dctx,m);
d4473b
	}
d4473b
d4473b
	return SLBT_CUSTOM_ERROR(dctx,SLBT_ERR_FLOW_ERROR);
d4473b
}
d4473b
4b56de
slbt_hidden struct ar_meta_member_info * slbt_archive_member_from_offset(
8567ec
	struct slbt_archive_meta_impl * meta,
8567ec
	off_t                           offset)
8567ec
{
8567ec
	intptr_t l,r,m;
8567ec
	off_t *  offsetv;
8567ec
8567ec
	l = 0;
8567ec
	r = meta->nentries - 1;
8567ec
8567ec
	offsetv = meta->offsetv;
8567ec
8567ec
	while (l != r) {
8567ec
		m  = (l + r) / 2;
2f121c
		m += (l + r) % 2;
8567ec
8567ec
		if (offsetv[m] > offset) {
8567ec
			r = --m;
8567ec
		} else {
8567ec
			l = m;
8567ec
		}
8567ec
	}
8567ec
8567ec
	return (offsetv[l] == offset) ? meta->memberv[l] : 0;
8567ec
}
8567ec
51c276
int slbt_ar_get_archive_meta(
d4473b
	const struct slbt_driver_ctx *  dctx,
d4473b
	const struct slbt_raw_archive * archive,
d4473b
	struct slbt_archive_meta **     meta)
d4473b
{
d4473b
	const char *                    mark;
d4473b
	const char *                    cap;
d4473b
	struct slbt_archive_meta_impl * m;
d4473b
	const char *                    slash;
d4473b
	const char *                    ch;
d4473b
	const char *                    fldcap;
d4473b
	size_t				nelements;
d4473b
	uint64_t                        nentries;
8fc5e1
	uint64_t                        nmembers;
d4473b
	uint64_t                        stblsize;
d4473b
	uint64_t                        filesize;
d4473b
	uint64_t                        namelen;
d4473b
	uint64_t                        nameoff;
d4473b
	uint32_t			attr;
405946
	void *                          s_addr;
405946
	void *                          m_addr;
405946
	const char *                    s_ptr;
405946
	const char *                    m_ptr;
d4473b
	struct ar_raw_file_header *	arhdr;
d4473b
	struct ar_raw_file_header *	arlongnames;
d4473b
	struct ar_meta_member_info *    memberp;
d4473b
	char *				longnamep;
d4473b
	size_t				idx;
b6b8ce
	struct ar_meta_armap_ref_32 *   symrefs_32;
b6b8ce
	struct ar_meta_armap_ref_64 *   symrefs_64;
d4473b
	struct ar_header_info *		hdrinfov;
d4473b
	struct ar_header_info *		hdrinfov_cap;
d4473b
	struct ar_header_info *		hdrinfov_next;
d4473b
	struct ar_header_info		hdrinfobuf[AR_STACK_VECTOR_ELEMENTS];
d4473b
d4473b
	/* init */
d4473b
	hdrinfov     = hdrinfobuf;
d4473b
	hdrinfov_cap = &hdrinfobuf[AR_STACK_VECTOR_ELEMENTS];
d4473b
	nelements    = AR_STACK_VECTOR_ELEMENTS;
d4473b
d4473b
	memset(hdrinfobuf,0,sizeof(hdrinfobuf));
d4473b
d4473b
	mark = archive->map_addr;
d4473b
	cap  = &mark[archive->map_size];
d4473b
d4473b
	/* preliminary validation */
d4473b
	if (archive->map_size < sizeof(struct ar_raw_signature))
d4473b
		return SLBT_CUSTOM_ERROR(
d4473b
			dctx,
d4473b
			SLBT_ERR_AR_INVALID_SIGNATURE);
d4473b
d4473b
	else if (strncmp(mark,ar_signature,sizeof(struct ar_raw_signature)))
d4473b
		return SLBT_CUSTOM_ERROR(
d4473b
			dctx,
d4473b
			SLBT_ERR_AR_INVALID_SIGNATURE);
d4473b
d4473b
	/* alloc */
d4473b
	if (!(m = calloc(1,sizeof(*m))))
d4473b
		return SLBT_SYSTEM_ERROR(dctx,0);
d4473b
805470
	/* associated driver context */
805470
	m->dctx = dctx;
805470
d4473b
	/* archive map info */
d4473b
	m->armeta.r_archive.map_addr = archive->map_addr;
d4473b
	m->armeta.r_archive.map_size = archive->map_size;
d4473b
d4473b
	/* archive signature */
d4473b
	m->armeta.r_signature = (struct ar_raw_signature *)mark;
d4473b
	m->armeta.m_signature = (struct ar_meta_signature *)ar_signature;
d4473b
d4473b
	/* signature only? */
d4473b
	if (archive->map_size == sizeof(struct ar_raw_signature)) {
d4473b
		*meta = &m->armeta;
d4473b
		return 0;
d4473b
	}
d4473b
d4473b
	mark += sizeof(struct ar_raw_signature);
d4473b
d4473b
	/* only trailing null characters past the signature? */
d4473b
	if (cap < &mark[sizeof(*arhdr)])
d4473b
		for (ch=mark; ch
d4473b
			if (*ch)
51c276
				return slbt_ar_free_archive_meta_impl(
d4473b
					m,SLBT_CUSTOM_ERROR(
d4473b
						dctx,
d4473b
						SLBT_ERR_AR_INVALID_HEADER));
d4473b
d4473b
	/* count entries, calculate string table size */
d4473b
	for (nentries=0,stblsize=0,arlongnames=0; mark
d4473b
		arhdr = (struct ar_raw_file_header *)mark;
d4473b
d4473b
		/* file size */
d4473b
		if ((slbt_ar_read_decimal_64(
d4473b
				arhdr->ar_file_size,
d4473b
				sizeof(arhdr->ar_file_size),
d4473b
				&filesize)) < 0)
51c276
			return slbt_ar_free_archive_meta_impl(
d4473b
				m,SLBT_CUSTOM_ERROR(
d4473b
					dctx,
d4473b
					SLBT_ERR_AR_INVALID_HEADER));
d4473b
d4473b
		mark += sizeof(struct ar_raw_file_header);
d4473b
d4473b
		/* stblsize, member name type */
d4473b
		fldcap = &arhdr->ar_file_id[sizeof(arhdr->ar_file_id)];
d4473b
d4473b
		/* sysv long names table? */
d4473b
		if ((arhdr->ar_file_id[0] == '/') && (arhdr->ar_file_id[1] == '/')) {
d4473b
			for (ch=&arhdr->ar_file_id[2]; ch
d4473b
				if (*ch != AR_DEC_PADDING)
51c276
					return slbt_ar_free_archive_meta_impl(
d4473b
						m,SLBT_CUSTOM_ERROR(
d4473b
							dctx,
d4473b
							SLBT_ERR_AR_INVALID_HEADER));
d4473b
d4473b
			if (slbt_ar_read_decimal_64(
d4473b
					arhdr->ar_file_size,
d4473b
					sizeof(arhdr->ar_file_size),
d4473b
					&namelen) < 0)
51c276
				return slbt_ar_free_archive_meta_impl(
d4473b
					m,SLBT_CUSTOM_ERROR(
d4473b
						dctx,
d4473b
						SLBT_ERR_AR_INVALID_HEADER));
d4473b
d4473b
d4473b
			/* duplicate long names member? */
d4473b
			if (arlongnames)
51c276
				return slbt_ar_free_archive_meta_impl(
d4473b
					m,SLBT_CUSTOM_ERROR(
d4473b
						dctx,
d4473b
						SLBT_ERR_AR_DUPLICATE_LONG_NAMES));
d4473b
d4473b
			attr = AR_HEADER_ATTR_FILE_ID | AR_HEADER_ATTR_SYSV;
d4473b
d4473b
			stblsize++;
d4473b
			stblsize++;
d4473b
			stblsize++;
d4473b
d4473b
			stblsize += namelen;
d4473b
d4473b
			arlongnames = arhdr;
d4473b
1af4bb
		/* the /SYM64/ string must be special cased, also below when it gets copied */
1af4bb
		} else if (!strncmp(arhdr->ar_file_id,"/SYM64/",7)) {
1af4bb
			for (ch=&arhdr->ar_file_id[7]; ch
1af4bb
				if (*ch != AR_DEC_PADDING)
51c276
					return slbt_ar_free_archive_meta_impl(
1af4bb
						m,SLBT_CUSTOM_ERROR(
1af4bb
							dctx,
1af4bb
							SLBT_ERR_AR_INVALID_HEADER));
1af4bb
1af4bb
			attr      = AR_HEADER_ATTR_FILE_ID | AR_HEADER_ATTR_SYSV;
1af4bb
			stblsize += 8;
1af4bb
d4473b
		/* sysv armap member or sysv long name reference? */
d4473b
		} else if (arhdr->ar_file_id[0] == '/') {
d4473b
			if (slbt_ar_read_decimal_64(
d4473b
					&arhdr->ar_file_id[1],
d4473b
					sizeof(arhdr->ar_file_id)-1,
d4473b
					&nameoff) < 0)
51c276
				return slbt_ar_free_archive_meta_impl(
d4473b
					m,SLBT_CUSTOM_ERROR(
d4473b
						dctx,
d4473b
						SLBT_ERR_AR_INVALID_HEADER));
d4473b
d4473b
			if (arhdr->ar_file_id[1] == AR_DEC_PADDING) {
d4473b
				attr = AR_HEADER_ATTR_FILE_ID | AR_HEADER_ATTR_SYSV;
d4473b
				stblsize++;
d4473b
				stblsize++;
d4473b
			} else {
d4473b
				attr = AR_HEADER_ATTR_NAME_REF | AR_HEADER_ATTR_SYSV;
d4473b
			}
d4473b
d4473b
		/* bsd long name reference? */
d4473b
		} else if ((arhdr->ar_file_id[0] == '#')
d4473b
				&& (arhdr->ar_file_id[1] == '1')
d4473b
				&& (arhdr->ar_file_id[2] == '/')) {
d4473b
			if (slbt_ar_read_decimal_64(
d4473b
					&arhdr->ar_file_id[3],
d4473b
					sizeof(arhdr->ar_file_id)-3,
d4473b
					&namelen) < 0)
51c276
				return slbt_ar_free_archive_meta_impl(
d4473b
					m,SLBT_CUSTOM_ERROR(
d4473b
						dctx,
d4473b
						SLBT_ERR_AR_INVALID_HEADER));
d4473b
d4473b
			attr = AR_HEADER_ATTR_NAME_REF | AR_HEADER_ATTR_BSD;
d4473b
d4473b
			stblsize += namelen + 1;
d4473b
d4473b
		/* must be either a sysv short member name, or a (legacy) bsd short name */
d4473b
		} else {
d4473b
			for (ch=arhdr->ar_file_id,slash=0; (ch
d4473b
				if (*ch == '/')
d4473b
					slash = ch;
d4473b
d4473b
			if (slash) {
d4473b
				attr      = AR_HEADER_ATTR_FILE_ID | AR_HEADER_ATTR_SYSV;
d4473b
				stblsize += (slash - arhdr->ar_file_id) + 1;
d4473b
			} else {
d4473b
				attr      = AR_HEADER_ATTR_FILE_ID | AR_HEADER_ATTR_BSD;
d4473b
				stblsize += sizeof(arhdr->ar_file_id) + 1;
d4473b
			}
d4473b
d4473b
			for (; ch
d4473b
				if (*ch++ != AR_DEC_PADDING)
51c276
					return slbt_ar_free_archive_meta_impl(
d4473b
						m,SLBT_CUSTOM_ERROR(
d4473b
							dctx,
d4473b
							SLBT_ERR_AR_INVALID_HEADER));
d4473b
d4473b
		}
d4473b
d4473b
		/* truncated data? */
d4473b
		if (cap < &mark[filesize])
51c276
			return slbt_ar_free_archive_meta_impl(
d4473b
				m,SLBT_CUSTOM_ERROR(
d4473b
					dctx,
d4473b
					SLBT_ERR_AR_TRUNCATED_DATA));
d4473b
d4473b
		/* ar member alignment */
d4473b
		filesize += 1;
d4473b
		filesize |= 1;
d4473b
		filesize ^= 1;
d4473b
d4473b
		mark += filesize;
d4473b
d4473b
		/* only trailing null characters past the signature? */
d4473b
		if (cap < &mark[sizeof(*arhdr)])
d4473b
			for (; mark
d4473b
				if (*mark++)
51c276
					return slbt_ar_free_archive_meta_impl(
d4473b
						m,SLBT_CUSTOM_ERROR(
d4473b
							dctx,
d4473b
							SLBT_ERR_AR_INVALID_HEADER));
d4473b
d4473b
		/* transient header info vector */
d4473b
		if (&hdrinfov[nentries] == hdrinfov_cap) {
d4473b
			nelements = (nelements == AR_STACK_VECTOR_ELEMENTS)
1b515c
				? (nelements << 4) : (nelements << 1);
d4473b
d4473b
			if (!(hdrinfov_next = calloc(nelements,sizeof(*hdrinfov))))
51c276
				return slbt_ar_free_archive_meta_impl(
d4473b
					m,SLBT_CUSTOM_ERROR(
d4473b
						dctx,
d4473b
						SLBT_ERR_AR_TRUNCATED_DATA));
d4473b
d4473b
			for (idx=0; idx
d4473b
				hdrinfov_next[idx].phdr = hdrinfov[idx].phdr;
d4473b
				hdrinfov_next[idx].attr = hdrinfov[idx].attr;
d4473b
			};
d4473b
d4473b
			if (hdrinfov != hdrinfobuf)
d4473b
				free(hdrinfov);
d4473b
d4473b
			hdrinfov     = hdrinfov_next;
d4473b
			hdrinfov_cap = &hdrinfov_next[nelements];
d4473b
			m->hdrinfov  = hdrinfov;
d4473b
		}
d4473b
d4473b
		hdrinfov[nentries].phdr = arhdr;
d4473b
		hdrinfov[nentries].attr = attr;
d4473b
	}
d4473b
d4473b
	/* allocate name strings, member vector */
d4473b
	if (!(m->namestrs = calloc(1,stblsize)))
51c276
		return slbt_ar_free_archive_meta_impl(
d4473b
			m,SLBT_SYSTEM_ERROR(dctx,0));
d4473b
405946
	if (!(m->offsetv = calloc(nentries+1,sizeof(*m->offsetv))))
51c276
		return slbt_ar_free_archive_meta_impl(
405946
			m,SLBT_SYSTEM_ERROR(dctx,0));
405946
d4473b
	if (!(m->memberv = calloc(nentries+1,sizeof(*m->memberv))))
51c276
		return slbt_ar_free_archive_meta_impl(
d4473b
			m,SLBT_SYSTEM_ERROR(dctx,0));
d4473b
d4473b
	if (!(m->members = calloc(nentries,sizeof(*m->members))))
51c276
		return slbt_ar_free_archive_meta_impl(
d4473b
			m,SLBT_SYSTEM_ERROR(dctx,0));
d4473b
405946
	/* archive signature reference */
405946
	s_addr = archive->map_addr;
405946
	s_ptr  = s_addr;
405946
d4473b
	/* iterate, store meta data in library-friendly form */
d4473b
	for (idx=0,longnamep=m->namestrs; idx
d4473b
		arhdr           = hdrinfov[idx].phdr;
d4473b
		attr            = hdrinfov[idx].attr;
d4473b
405946
		m_addr          = arhdr;
405946
		m_ptr           = m_addr;
405946
d4473b
		memberp         = &m->members[idx];
405946
		m->offsetv[idx] = m_ptr - s_ptr;
d4473b
		m->memberv[idx] = memberp;
d4473b
d4473b
		memberp->ar_file_header.ar_header_attr = attr;
d4473b
d4473b
		slbt_ar_read_decimal_64(
d4473b
			arhdr->ar_time_date_stamp,
d4473b
			sizeof(arhdr->ar_time_date_stamp),
d4473b
			&memberp->ar_file_header.ar_time_date_stamp);
d4473b
d4473b
		slbt_ar_read_decimal_32(
d4473b
			arhdr->ar_uid,
d4473b
			sizeof(arhdr->ar_uid),
d4473b
			&memberp->ar_file_header.ar_uid);
d4473b
d4473b
		slbt_ar_read_decimal_32(
d4473b
			arhdr->ar_gid,
d4473b
			sizeof(arhdr->ar_gid),
d4473b
			&memberp->ar_file_header.ar_gid);
d4473b
d4473b
		slbt_ar_read_octal(
d4473b
			arhdr->ar_file_mode,
d4473b
			sizeof(arhdr->ar_file_mode),
d4473b
			&memberp->ar_file_header.ar_file_mode);
d4473b
d4473b
		slbt_ar_read_decimal_64(
d4473b
			arhdr->ar_file_size,
d4473b
			sizeof(arhdr->ar_file_size),
d4473b
			&memberp->ar_file_header.ar_file_size);
d4473b
d4473b
		memberp->ar_file_header.ar_member_name = longnamep;
d4473b
d4473b
		if (attr == (AR_HEADER_ATTR_FILE_ID | AR_HEADER_ATTR_SYSV)) {
d4473b
			if ((arhdr->ar_file_id[0] == '/') && (arhdr->ar_file_id[1] == '/')) {
d4473b
				*longnamep++ = '/';
d4473b
				*longnamep++ = '/';
d4473b
				longnamep++;
d4473b
1af4bb
			} else if ((arhdr->ar_file_id[0] == '/') && (arhdr->ar_file_id[1] == 'S')) {
1af4bb
				*longnamep++ = '/';
1af4bb
				*longnamep++ = 'S';
1af4bb
				*longnamep++ = 'Y';
1af4bb
				*longnamep++ = 'M';
1af4bb
				*longnamep++ = '6';
1af4bb
				*longnamep++ = '4';
1af4bb
				*longnamep++ = '/';
1af4bb
				longnamep++;
1af4bb
d4473b
			} else if (arhdr->ar_file_id[0] == '/') {
d4473b
				*longnamep++ = '/';
d4473b
				longnamep++;
d4473b
d4473b
			} else {
d4473b
				ch = arhdr->ar_file_id;
d4473b
d4473b
				for (; (*ch != '/'); )
d4473b
					*longnamep++ = *ch++;
d4473b
d4473b
				longnamep++;
d4473b
			}
d4473b
d4473b
		} else if (attr == (AR_HEADER_ATTR_FILE_ID | AR_HEADER_ATTR_BSD)) {
d4473b
			ch     = arhdr->ar_file_id;
d4473b
			fldcap = &ch[sizeof(arhdr->ar_file_id)];
d4473b
d4473b
			for (; (ch
d4473b
				*longnamep++ = *ch++;
d4473b
d4473b
			longnamep++;
d4473b
d4473b
		} else if (attr == (AR_HEADER_ATTR_NAME_REF | AR_HEADER_ATTR_SYSV)) {
d4473b
			slbt_ar_read_decimal_64(
d4473b
				&arhdr->ar_file_id[1],
d4473b
				sizeof(arhdr->ar_file_id) - 1,
d4473b
				&nameoff);
d4473b
d4473b
			ch  = arlongnames->ar_file_id;
d4473b
			ch += sizeof(*arlongnames);
d4473b
			ch += nameoff;
d4473b
d4473b
			for (; *ch && (*ch != '/') && (*ch != AR_OBJ_PADDING); )
d4473b
				*longnamep++ = *ch++;
d4473b
d4473b
			longnamep++;
d4473b
d4473b
		} else if (attr == (AR_HEADER_ATTR_NAME_REF | AR_HEADER_ATTR_BSD)) {
d4473b
			slbt_ar_read_decimal_64(
d4473b
				&arhdr->ar_file_id[3],
d4473b
				sizeof(arhdr->ar_file_id) - 3,
d4473b
				&namelen);
d4473b
d4473b
			mark  = arhdr->ar_file_id;
d4473b
			mark += sizeof(*arhdr);
d4473b
d4473b
			memcpy(longnamep,mark,namelen);
d4473b
d4473b
			longnamep += namelen;
d4473b
			longnamep++;
d4473b
		}
d4473b
335caf
		/* member raw header, object size, object data */
d4473b
		mark    = arhdr->ar_file_id;
d4473b
		mark   += sizeof(*arhdr);
d4473b
		namelen = 0;
d4473b
d4473b
		if (attr == (AR_HEADER_ATTR_NAME_REF | AR_HEADER_ATTR_BSD)) {
d4473b
			slbt_ar_read_decimal_64(
d4473b
				&arhdr->ar_file_id[3],
d4473b
				sizeof(arhdr->ar_file_id)-3,
d4473b
				&namelen);
d4473b
d4473b
			namelen += 1;
d4473b
			namelen |= 1;
d4473b
			namelen ^= 1;
d4473b
d4473b
			mark += namelen;
d4473b
		};
d4473b
335caf
		memberp->ar_member_data = arhdr;
d4473b
		memberp->ar_object_data = (void *)mark;
d4473b
		memberp->ar_object_size = memberp->ar_file_header.ar_file_size - namelen;
d4473b
d4473b
		/* member attribute */
d4473b
		memberp->ar_member_attr = slbt_ar_get_member_attr(memberp);
d4473b
d4473b
		/* pe/coff second linker member? */
d4473b
		if ((idx == 1) && (memberp->ar_member_attr == AR_MEMBER_ATTR_ARMAP))
d4473b
			if (hdrinfov[0].attr & AR_HEADER_ATTR_SYSV)
d4473b
				if (m->members[0].ar_member_attr == AR_MEMBER_ATTR_ARMAP)
d4473b
					if (attr & AR_HEADER_ATTR_SYSV)
d4473b
						memberp->ar_member_attr = AR_MEMBER_ATTR_LINKINFO;
d4473b
d4473b
		/* armap member must be the first */
d4473b
		if ((memberp->ar_member_attr == AR_MEMBER_ATTR_ARMAP) && (idx > 0)) {
d4473b
			if (m->members[0].ar_member_attr == AR_MEMBER_ATTR_ARMAP)
51c276
				return slbt_ar_free_archive_meta_impl(
d4473b
					m,SLBT_CUSTOM_ERROR(
d4473b
						dctx,
d4473b
						SLBT_ERR_AR_DUPLICATE_ARMAP_MEMBER));
d4473b
51c276
			return slbt_ar_free_archive_meta_impl(
d4473b
				m,SLBT_CUSTOM_ERROR(
d4473b
					dctx,
d4473b
					SLBT_ERR_AR_MISPLACED_ARMAP_MEMBER));
d4473b
		}
d4473b
	}
d4473b
1f3f88
	/* number of archive members, including internal ones */
1f3f88
	m->nentries = nentries;
1f3f88
d4473b
	/* primary armap (first linker member) */
d4473b
	if (slbt_ar_parse_primary_armap(dctx,m) < 0)
51c276
		return slbt_ar_free_archive_meta_impl(
d4473b
			m,SLBT_NESTED_ERROR(dctx));
d4473b
d4473b
	for (idx=0,ch=m->symstrs; idx<m->armaps.armap_nsyms; idx++) {
d4473b
		m->symstrv[idx] = ch;
d4473b
		ch += strlen(ch);
d4473b
		ch++;
d4473b
	}
d4473b
b6b8ce
	if (m->armaps.armap_common_32.ar_member) {
b6b8ce
		symrefs_32 = m->armaps.armap_symrefs_32;
b6b8ce
1f3f88
		for (idx=0; idx<m->armaps.armap_nsyms; idx++) {
4223c8
			if (m->armaps.armap_common_32.ar_armap_attr & AR_ARMAP_ATTR_SYSV)
4223c8
				symrefs_32[idx].ar_name_offset = m->symstrv[idx] - m->symstrv[0];
1f3f88
1f3f88
			if (!slbt_archive_member_from_offset(m,symrefs_32[idx].ar_member_offset))
51c276
				return slbt_ar_free_archive_meta_impl(
1f3f88
					m,SLBT_CUSTOM_ERROR(
1f3f88
						dctx,
1f3f88
						SLBT_ERR_AR_INVALID_ARMAP_MEMBER_OFFSET));
1f3f88
5ac87e
			if (symrefs_32[idx].ar_name_offset) {
5ac87e
				ch = &m->symstrs[symrefs_32[idx].ar_name_offset];
5ac87e
5ac87e
				if ((ch > m->symstrv[m->armaps.armap_nsyms - 1]) || *--ch)
51c276
					return slbt_ar_free_archive_meta_impl(
5ac87e
						m,SLBT_CUSTOM_ERROR(
5ac87e
							dctx,
5ac87e
							SLBT_ERR_AR_INVALID_ARMAP_NAME_OFFSET));
5ac87e
			}
5ac87e
1f3f88
		}
b6b8ce
	}
b6b8ce
b6b8ce
	if (m->armaps.armap_common_64.ar_member) {
b6b8ce
		symrefs_64 = m->armaps.armap_symrefs_64;
b6b8ce
1f3f88
		for (idx=0; idx<m->armaps.armap_nsyms; idx++) {
4223c8
			if (m->armaps.armap_common_64.ar_armap_attr & AR_ARMAP_ATTR_SYSV)
4223c8
				symrefs_64[idx].ar_name_offset = m->symstrv[idx] - m->symstrv[0];
1f3f88
1f3f88
			if (!slbt_archive_member_from_offset(m,symrefs_64[idx].ar_member_offset))
51c276
				return slbt_ar_free_archive_meta_impl(
1f3f88
					m,SLBT_CUSTOM_ERROR(
1f3f88
						dctx,
1f3f88
						SLBT_ERR_AR_INVALID_ARMAP_MEMBER_OFFSET));
5ac87e
5ac87e
			if (symrefs_64[idx].ar_name_offset) {
5ac87e
				ch = &m->symstrs[symrefs_64[idx].ar_name_offset];
5ac87e
5ac87e
				if ((ch > m->symstrv[m->armaps.armap_nsyms - 1]) || *--ch)
51c276
					return slbt_ar_free_archive_meta_impl(
5ac87e
						m,SLBT_CUSTOM_ERROR(
5ac87e
							dctx,
5ac87e
							SLBT_ERR_AR_INVALID_ARMAP_NAME_OFFSET));
5ac87e
			}
5ac87e
1f3f88
		}
b6b8ce
	}
b6b8ce
8fc5e1
	/* number of public archive members */
8fc5e1
	for (idx=0,nmembers=0; idx
8fc5e1
		switch (m->memberv[idx]->ar_member_attr) {
8fc5e1
			case AR_MEMBER_ATTR_ARMAP:
8fc5e1
			case AR_MEMBER_ATTR_LINKINFO:
8fc5e1
			case AR_MEMBER_ATTR_NAMESTRS:
8fc5e1
				break;
8fc5e1
8fc5e1
			default:
8fc5e1
				nmembers++;
8fc5e1
		}
8fc5e1
	}
8fc5e1
8fc5e1
	if (m->armaps.armap_common_32.ar_member)
8fc5e1
		m->armaps.armap_common_32.ar_num_of_members = nmembers;
8fc5e1
8fc5e1
	if (m->armaps.armap_common_64.ar_member)
8fc5e1
		m->armaps.armap_common_64.ar_num_of_members = nmembers;
8fc5e1
d4473b
	/* pe/coff armap attributes (second linker member) */
d4473b
	(void)m->armeta.a_armap_pecoff;
d4473b
8a77f6
	/* reference to the long names member */
8a77f6
	if (arlongnames)
8a77f6
		for (idx=0; idx<nentries && !m->armeta.a_arref_longnames; idx++)
8a77f6
			if (m->memberv[idx]->ar_member_data == arlongnames)
8a77f6
				m->armeta.a_arref_longnames = m->memberv[idx];
8a77f6
0006f7
	/* common binary format (information only) */
0006f7
	for (idx=0,nmembers=0; idx
0006f7
		if (m->memberv[idx]->ar_member_attr == AR_MEMBER_ATTR_OBJECT) {
0006f7
			if (m->ofmtattr && (m->ofmtattr != m->memberv[idx]->ar_object_attr)) {
0006f7
				m->ofmtattr = 0;
0006f7
				idx = nentries;
0006f7
			} else if (!m->ofmtattr) {
0006f7
				m->ofmtattr = m->memberv[idx]->ar_object_attr;
0006f7
			}
0006f7
		}
0006f7
	}
0006f7
ad78c3
	/* member vector */
ad78c3
	m->armeta.a_memberv = m->memberv;
ad78c3
d4473b
	/* all done */
d4473b
	if (m->hdrinfov) {
d4473b
		free(m->hdrinfov);
d4473b
		m->hdrinfov = 0;
d4473b
	}
d4473b
d4473b
	*meta = &m->armeta;
d4473b
d4473b
	return 0;
d4473b
}
d4473b
51c276
void slbt_ar_free_archive_meta(struct slbt_archive_meta * meta)
d4473b
{
d4473b
	struct slbt_archive_meta_impl * m;
d4473b
d4473b
	if (meta) {
d4473b
		m = slbt_archive_meta_ictx(meta);
51c276
		slbt_ar_free_archive_meta_impl(m,0);
d4473b
	}
d4473b
}