firasuke / cross / slibtool

Forked from cross/slibtool 3 months ago
Clone

Blame src/arbits/slbt_armap_bsd_32.c

5658da
/*******************************************************************/
eac61a
/*  slibtool: a strong libtool implementation, written in C        */
5658da
/*  Copyright (C) 2016--2024  SysDeer Technologies, LLC            */
5658da
/*  Released under the Standard MIT License; see COPYING.SLIBTOOL. */
5658da
/*******************************************************************/
5658da
5658da
#include <stdint.h>
5658da
#include <stddef.h>
5658da
#include <stdlib.h>
5658da
#include <string.h>
5658da
5658da
#include <slibtool/slibtool.h>
5658da
#include <slibtool/slibtool_arbits.h>
5658da
#include "slibtool_ar_impl.h"
5658da
#include "slibtool_driver_impl.h"
5658da
#include "slibtool_errinfo_impl.h"
4b56de
#include "slibtool_visibility_impl.h"
5658da
4b56de
slbt_hidden int slbt_ar_parse_primary_armap_bsd_32(
5658da
	const struct slbt_driver_ctx *  dctx,
5658da
	struct slbt_archive_meta_impl * m)
5658da
5658da
{
5658da
	struct ar_raw_armap_bsd_32 *    armap;
5658da
	struct ar_meta_member_info *    memberp;
5658da
	struct ar_meta_armap_common_32 *armapref;
5658da
	struct ar_meta_armap_ref_32 *   symrefs;
5658da
	uint32_t                        idx;
5658da
	uint32_t                        uref;
5658da
	uint32_t                        attr;
5658da
	uint32_t                        nsyms;
5658da
	uint32_t                        nstrs;
5658da
	uint32_t                        sizeofrefs_le;
5658da
	uint32_t                        sizeofrefs_be;
5658da
	uint32_t                        sizeofrefs;
5658da
	uint32_t                        sizeofstrs;
5658da
	const char *                    ch;
5658da
	const char *                    cap;
5658da
	unsigned char *                 uch;
5658da
	unsigned char                   (*mark)[0x04];
5658da
5658da
	armap   = &m->armaps.armap_bsd_32;
5658da
	memberp = m->memberv[0];
5658da
5658da
	mark = memberp->ar_object_data;
5658da
5658da
	armap->ar_size_of_refs = mark;
5658da
	uch = *mark++;
5658da
5658da
	armap->ar_first_name_offset = mark;
5658da
5658da
	sizeofrefs_le = (uch[3] << 24) + (uch[2] << 16) + (uch[1] << 8) + uch[0];
5658da
	sizeofrefs_be = (uch[0] << 24) + (uch[1] << 16) + (uch[2] << 8) + uch[3];
5658da
5658da
	if (sizeofrefs_le < memberp->ar_object_size - sizeof(*mark)) {
5658da
		sizeofrefs = sizeofrefs_le;
5658da
		attr       = AR_ARMAP_ATTR_LE_32;
5658da
5658da
	} else if (sizeofrefs_be < memberp->ar_object_size - sizeof(*mark)) {
5658da
		sizeofrefs = sizeofrefs_be;
5658da
		attr       = AR_ARMAP_ATTR_BE_32;
5658da
	} else {
5658da
		return SLBT_CUSTOM_ERROR(
5658da
			dctx,
5658da
			SLBT_ERR_AR_INVALID_ARMAP_SIZE_OF_REFS);
5658da
	}
5658da
5658da
	nsyms  = sizeofrefs / sizeof(struct ar_raw_armap_ref_32);
5658da
	mark  += (sizeofrefs / sizeof(*mark));
5658da
5658da
	armap->ar_size_of_strs = mark;
5658da
	uch = *mark++;
5658da
5658da
	sizeofstrs = (attr == AR_ARMAP_ATTR_LE_32)
5658da
		? (uch[3] << 24) + (uch[2] << 16) + (uch[1] << 8) + uch[0]
5658da
		: (uch[0] << 24) + (uch[1] << 16) + (uch[2] << 8) + uch[3];
5658da
5658da
	if (sizeofstrs > memberp->ar_object_size - 2*sizeof(*mark) - sizeofrefs)
5658da
		return SLBT_CUSTOM_ERROR(
5658da
			dctx,
5658da
			SLBT_ERR_AR_INVALID_ARMAP_SIZE_OF_STRS);
5658da
5658da
	m->symstrs = (const char *)mark;
5658da
5658da
	cap  = m->symstrs;
5658da
	cap += sizeofstrs;
5658da
5658da
	if ((cap == m->symstrs) && nsyms)
5658da
		return SLBT_CUSTOM_ERROR(
5658da
			dctx,
5658da
			SLBT_ERR_AR_INVALID_ARMAP_STRING_TABLE);
5658da
5658da
	if (nsyms && !m->symstrs[0])
5658da
		return SLBT_CUSTOM_ERROR(
5658da
			dctx,
5658da
			SLBT_ERR_AR_INVALID_ARMAP_STRING_TABLE);
5658da
5658da
	for (ch=&m->symstrs[1],nstrs=0; ch
5658da
		if (!ch[0] && !ch[-1] && (nstrs < nsyms))
5658da
			return SLBT_CUSTOM_ERROR(
5658da
				dctx,
5658da
				SLBT_ERR_AR_INVALID_ARMAP_STRING_TABLE);
5658da
5658da
		if (!ch[0] && ch[-1] && (nstrs < nsyms))
5658da
			nstrs++;
5658da
	}
5658da
5658da
	if (nstrs != nsyms)
5658da
		return SLBT_CUSTOM_ERROR(
5658da
			dctx,
5658da
			SLBT_ERR_AR_INVALID_ARMAP_STRING_TABLE);
5658da
5658da
	if (!(m->symstrv = calloc(nsyms + 1,sizeof(const char *))))
5658da
		return SLBT_SYSTEM_ERROR(dctx,0);
5658da
5658da
	if (!(m->armaps.armap_symrefs_32 = calloc(nsyms + 1,sizeof(*symrefs))))
5658da
		return SLBT_SYSTEM_ERROR(dctx,0);
5658da
5658da
	mark    = armap->ar_first_name_offset;
5658da
	symrefs = m->armaps.armap_symrefs_32;
5658da
4223c8
	for (idx=0; idx
4223c8
		uch  = *mark++;
4223c8
4223c8
		uref = (attr == AR_ARMAP_ATTR_BE_32)
4223c8
			? (uch[0] << 24) + (uch[1] << 16) + (uch[2] << 8) + uch[3]
4223c8
			: (uch[3] << 24) + (uch[2] << 16) + (uch[1] << 8) + uch[0];
4223c8
4223c8
		symrefs[idx].ar_name_offset = uref;
4223c8
4223c8
		uch  = *mark++;
4223c8
5658da
		uref = (attr == AR_ARMAP_ATTR_BE_32)
5658da
			? (uch[0] << 24) + (uch[1] << 16) + (uch[2] << 8) + uch[3]
5658da
			: (uch[3] << 24) + (uch[2] << 16) + (uch[1] << 8) + uch[0];
5658da
5658da
		symrefs[idx].ar_member_offset = uref;
5658da
	}
5658da
5658da
	armap->ar_string_table = m->symstrv;
5658da
5658da
	armapref = &m->armaps.armap_common_32;
5658da
	armapref->ar_member         = memberp;
5658da
	armapref->ar_symrefs        = symrefs;
5658da
	armapref->ar_armap_bsd      = armap;
5658da
	armapref->ar_armap_attr     = AR_ARMAP_ATTR_BSD | attr;
5658da
	armapref->ar_num_of_symbols = nsyms;
5658da
	armapref->ar_size_of_refs   = sizeofrefs;
5658da
	armapref->ar_size_of_strs   = sizeofstrs;
5658da
	armapref->ar_string_table   = m->symstrs;
5658da
5658da
	m->armaps.armap_nsyms = nsyms;
5658da
5658da
	m->armeta.a_armap_primary.ar_armap_common_32 = armapref;
5658da
5658da
	return 0;
5658da
}