Blame src/arbits/slbt_armap_bsd_64.c

dc33d4
/*******************************************************************/
eac61a
/*  slibtool: a strong libtool implementation, written in C        */
dc33d4
/*  Copyright (C) 2016--2024  SysDeer Technologies, LLC            */
dc33d4
/*  Released under the Standard MIT License; see COPYING.SLIBTOOL. */
dc33d4
/*******************************************************************/
dc33d4
dc33d4
#include <stdint.h>
dc33d4
#include <stddef.h>
dc33d4
#include <stdlib.h>
dc33d4
#include <string.h>
dc33d4
dc33d4
#include <slibtool/slibtool.h>
dc33d4
#include <slibtool/slibtool_arbits.h>
dc33d4
#include "slibtool_ar_impl.h"
dc33d4
#include "slibtool_driver_impl.h"
dc33d4
#include "slibtool_errinfo_impl.h"
4b56de
#include "slibtool_visibility_impl.h"
dc33d4
4b56de
slbt_hidden int slbt_ar_parse_primary_armap_bsd_64(
dc33d4
	const struct slbt_driver_ctx *  dctx,
dc33d4
	struct slbt_archive_meta_impl * m)
dc33d4
{
dc33d4
	struct ar_raw_armap_bsd_64 *    armap;
dc33d4
	struct ar_meta_member_info *    memberp;
dc33d4
	struct ar_meta_armap_common_64 *armapref;
dc33d4
	struct ar_meta_armap_ref_64 *   symrefs;
dc33d4
	uint64_t                        idx;
dc33d4
	uint64_t                        uref_hi;
dc33d4
	uint64_t                        uref_lo;
dc33d4
	uint32_t                        attr;
dc33d4
	uint64_t                        u64_lo;
dc33d4
	uint64_t                        u64_hi;
dc33d4
	uint64_t                        nsyms;
dc33d4
	uint64_t                        nstrs;
dc33d4
	uint64_t                        sizeofrefs_le;
dc33d4
	uint64_t                        sizeofrefs_be;
dc33d4
	uint64_t                        sizeofrefs;
dc33d4
	uint64_t                        sizeofstrs;
dc33d4
	const char *                    ch;
dc33d4
	const char *                    cap;
dc33d4
	unsigned char *                 uch;
dc33d4
	unsigned char                   (*mark)[0x08];
dc33d4
dc33d4
	armap   = &m->armaps.armap_bsd_64;
dc33d4
	memberp = m->memberv[0];
dc33d4
dc33d4
	mark = memberp->ar_object_data;
dc33d4
dc33d4
	armap->ar_size_of_refs = mark;
dc33d4
	uch = *mark++;
dc33d4
dc33d4
	armap->ar_first_name_offset = mark;
dc33d4
dc33d4
	u64_lo = (uch[3] << 24) + (uch[2] << 16) + (uch[1] << 8) + uch[0];
dc33d4
	u64_hi = (uch[7] << 24) + (uch[6] << 16) + (uch[5] << 8) + uch[4];
dc33d4
dc33d4
	sizeofrefs_le = u64_lo + (u64_hi << 32);
dc33d4
dc33d4
	u64_hi = (uch[0] << 24) + (uch[1] << 16) + (uch[2] << 8) + uch[3];
dc33d4
	u64_lo = (uch[4] << 24) + (uch[5] << 16) + (uch[6] << 8) + uch[7];
dc33d4
dc33d4
	sizeofrefs_be = (u64_hi << 32) + u64_lo;
dc33d4
dc33d4
	if (sizeofrefs_le < memberp->ar_object_size - sizeof(*mark)) {
dc33d4
		sizeofrefs = sizeofrefs_le;
dc33d4
		attr       = AR_ARMAP_ATTR_LE_64;
dc33d4
dc33d4
	} else if (sizeofrefs_be < memberp->ar_object_size - sizeof(*mark)) {
dc33d4
		sizeofrefs = sizeofrefs_be;
dc33d4
		attr       = AR_ARMAP_ATTR_BE_64;
dc33d4
	} else {
dc33d4
		return SLBT_CUSTOM_ERROR(
dc33d4
			dctx,
dc33d4
			SLBT_ERR_AR_INVALID_ARMAP_SIZE_OF_REFS);
dc33d4
	}
dc33d4
dc33d4
	nsyms  = sizeofrefs / sizeof(struct ar_raw_armap_ref_64);
dc33d4
	mark  += (sizeofrefs / sizeof(*mark));
dc33d4
dc33d4
	armap->ar_size_of_strs = mark;
dc33d4
	uch = *mark++;
dc33d4
dc33d4
	if (attr == AR_ARMAP_ATTR_LE_64) {
dc33d4
		u64_lo = (uch[3] << 24) + (uch[2] << 16) + (uch[1] << 8) + uch[0];
dc33d4
		u64_hi = (uch[7] << 24) + (uch[6] << 16) + (uch[5] << 8) + uch[4];
dc33d4
	} else {
dc33d4
		u64_hi = (uch[0] << 24) + (uch[1] << 16) + (uch[2] << 8) + uch[3];
dc33d4
		u64_lo = (uch[4] << 24) + (uch[5] << 16) + (uch[6] << 8) + uch[7];
dc33d4
	}
dc33d4
dc33d4
	sizeofstrs = u64_lo + (u64_hi << 32);
dc33d4
	m->symstrs = (const char *)mark;
dc33d4
dc33d4
	cap  = m->symstrs;
dc33d4
	cap += sizeofstrs;
dc33d4
dc33d4
	if ((cap == m->symstrs) && nsyms)
dc33d4
		return SLBT_CUSTOM_ERROR(
dc33d4
			dctx,
dc33d4
			SLBT_ERR_AR_INVALID_ARMAP_STRING_TABLE);
dc33d4
dc33d4
	if (nsyms && !m->symstrs[0])
dc33d4
		return SLBT_CUSTOM_ERROR(
dc33d4
			dctx,
dc33d4
			SLBT_ERR_AR_INVALID_ARMAP_STRING_TABLE);
dc33d4
dc33d4
	for (ch=&m->symstrs[1],nstrs=0; ch
dc33d4
		if (!ch[0] && !ch[-1] && (nstrs < nsyms))
dc33d4
			return SLBT_CUSTOM_ERROR(
dc33d4
				dctx,
dc33d4
				SLBT_ERR_AR_INVALID_ARMAP_STRING_TABLE);
dc33d4
dc33d4
		if (!ch[0] && ch[-1] && (nstrs < nsyms))
dc33d4
			nstrs++;
dc33d4
	}
dc33d4
dc33d4
	if (nstrs != nsyms)
dc33d4
		return SLBT_CUSTOM_ERROR(
dc33d4
			dctx,
dc33d4
			SLBT_ERR_AR_INVALID_ARMAP_STRING_TABLE);
dc33d4
dc33d4
	if (!(m->symstrv = calloc(nsyms + 1,sizeof(const char *))))
dc33d4
		return SLBT_SYSTEM_ERROR(dctx,0);
dc33d4
dc33d4
	if (!(m->armaps.armap_symrefs_64 = calloc(nsyms + 1,sizeof(*symrefs))))
dc33d4
		return SLBT_SYSTEM_ERROR(dctx,0);
dc33d4
dc33d4
	mark    = armap->ar_first_name_offset;
dc33d4
	symrefs = m->armaps.armap_symrefs_64;
dc33d4
4223c8
	for (idx=0; idx
4223c8
		uch  = *mark++;
4223c8
4223c8
		if (attr == AR_ARMAP_ATTR_BE_64) {
4223c8
			uref_hi = (uch[0] << 24) + (uch[1] << 16) + (uch[2] << 8) + uch[3];
4223c8
			uref_lo = (uch[4] << 24) + (uch[5] << 16) + (uch[6] << 8) + uch[7];
4223c8
		} else {
4223c8
			uref_lo = (uch[3] << 24) + (uch[2] << 16) + (uch[1] << 8) + uch[0];
4223c8
			uref_hi = (uch[7] << 24) + (uch[6] << 16) + (uch[5] << 8) + uch[4];
4223c8
		}
4223c8
4223c8
		symrefs[idx].ar_name_offset = (uref_hi << 32) + uref_lo;
4223c8
4223c8
		uch  = *mark++;
4223c8
dc33d4
		if (attr == AR_ARMAP_ATTR_BE_64) {
dc33d4
			uref_hi = (uch[0] << 24) + (uch[1] << 16) + (uch[2] << 8) + uch[3];
dc33d4
			uref_lo = (uch[4] << 24) + (uch[5] << 16) + (uch[6] << 8) + uch[7];
dc33d4
		} else {
dc33d4
			uref_lo = (uch[3] << 24) + (uch[2] << 16) + (uch[1] << 8) + uch[0];
dc33d4
			uref_hi = (uch[7] << 24) + (uch[6] << 16) + (uch[5] << 8) + uch[4];
dc33d4
		}
dc33d4
dc33d4
		symrefs[idx].ar_member_offset = (uref_hi << 32) + uref_lo;
dc33d4
	}
dc33d4
dc33d4
	armap->ar_string_table = m->symstrv;
dc33d4
dc33d4
	armapref = &m->armaps.armap_common_64;
dc33d4
	armapref->ar_member         = memberp;
dc33d4
	armapref->ar_symrefs        = symrefs;
dc33d4
	armapref->ar_armap_bsd      = armap;
dc33d4
	armapref->ar_armap_attr     = AR_ARMAP_ATTR_BSD | attr;
dc33d4
	armapref->ar_num_of_symbols = nsyms;
dc33d4
	armapref->ar_size_of_refs   = sizeofrefs;
dc33d4
	armapref->ar_size_of_strs   = sizeofstrs;
dc33d4
	armapref->ar_string_table   = m->symstrs;
dc33d4
dc33d4
	m->armaps.armap_nsyms = nsyms;
dc33d4
dc33d4
	m->armeta.a_armap_primary.ar_armap_common_64 = armapref;
dc33d4
dc33d4
	return 0;
dc33d4
}