Blame src/arbits/slbt_armap_sysv_32.c

dfb348
/*******************************************************************/
dfb348
/*  slibtool: a skinny libtool implementation, written in C        */
dfb348
/*  Copyright (C) 2016--2024  SysDeer Technologies, LLC            */
dfb348
/*  Released under the Standard MIT License; see COPYING.SLIBTOOL. */
dfb348
/*******************************************************************/
dfb348
dfb348
#include <stdint.h>
dfb348
#include <stddef.h>
dfb348
#include <stdlib.h>
dfb348
#include <string.h>
dfb348
dfb348
#include <slibtool/slibtool.h>
dfb348
#include <slibtool/slibtool_arbits.h>
dfb348
#include "slibtool_ar_impl.h"
dfb348
#include "slibtool_driver_impl.h"
dfb348
#include "slibtool_errinfo_impl.h"
dfb348
dfb348
int slbt_ar_parse_primary_armap_sysv_32(
dfb348
	const struct slbt_driver_ctx *  dctx,
dfb348
	struct slbt_archive_meta_impl * m)
dfb348
{
dfb348
	struct ar_raw_armap_sysv_32 *   armap;
dfb348
	struct ar_meta_member_info *    memberp;
dfb348
	struct ar_meta_armap_common_32 *armapref;
dfb348
	struct ar_meta_armap_ref_32 *   symrefs;
dfb348
	uint32_t                        idx;
dfb348
	uint32_t                        uref;
dfb348
	uint32_t                        nsyms;
dfb348
	uint32_t                        nstrs;
dfb348
	const char *                    ch;
dfb348
	const char *                    cap;
dfb348
	unsigned char *                 uch;
dfb348
	unsigned char                   (*mark)[0x04];
dfb348
dfb348
	armap   = &m->armaps.armap_sysv_32;
dfb348
	memberp = m->memberv[0];
dfb348
dfb348
	mark = memberp->ar_object_data;
dfb348
dfb348
	armap->ar_num_of_syms = mark;
dfb348
	uch = *mark++;
dfb348
dfb348
	armap->ar_first_ref_offset = mark;
dfb348
dfb348
	nsyms = (uch[0] << 24) + (uch[1] << 16) + (uch[2] << 8) + uch[3];
dfb348
	mark += nsyms;
dfb348
dfb348
	if (memberp->ar_object_size < (sizeof(*mark) + (nsyms * sizeof(*mark))))
dfb348
		return SLBT_CUSTOM_ERROR(
dfb348
			dctx,
dfb348
			SLBT_ERR_AR_INVALID_ARMAP_NUMBER_OF_SYMS);
dfb348
dfb348
	m->symstrs = (const char *)mark;
dfb348
dfb348
	cap  = memberp->ar_object_data;
dfb348
	cap += memberp->ar_object_size;
dfb348
dfb348
	if ((cap == m->symstrs) && nsyms)
dfb348
		return SLBT_CUSTOM_ERROR(
dfb348
			dctx,
dfb348
			SLBT_ERR_AR_INVALID_ARMAP_STRING_TABLE);
dfb348
dfb348
	if (nsyms && !m->symstrs[0])
dfb348
		return SLBT_CUSTOM_ERROR(
dfb348
			dctx,
dfb348
			SLBT_ERR_AR_INVALID_ARMAP_STRING_TABLE);
dfb348
dfb348
	for (ch=&m->symstrs[1],nstrs=0; ch
dfb348
		if (!ch[0] && !ch[-1] && (nstrs < nsyms))
dfb348
			return SLBT_CUSTOM_ERROR(
dfb348
				dctx,
dfb348
				SLBT_ERR_AR_INVALID_ARMAP_STRING_TABLE);
dfb348
dfb348
		if (!ch[0] && ch[-1])
dfb348
			nstrs++;
dfb348
	}
dfb348
dfb348
	if (nstrs != nsyms)
dfb348
		return SLBT_CUSTOM_ERROR(
dfb348
			dctx,
dfb348
			SLBT_ERR_AR_INVALID_ARMAP_STRING_TABLE);
dfb348
dfb348
	if (cap[-1])
dfb348
		return SLBT_CUSTOM_ERROR(
dfb348
			dctx,
dfb348
			SLBT_ERR_AR_INVALID_ARMAP_STRING_TABLE);
dfb348
dfb348
	if (!(m->symstrv = calloc(nsyms + 1,sizeof(const char *))))
dfb348
		return SLBT_SYSTEM_ERROR(dctx,0);
dfb348
dfb348
	if (!(m->armaps.armap_symrefs_32 = calloc(nsyms + 1,sizeof(*symrefs))))
dfb348
		return SLBT_SYSTEM_ERROR(dctx,0);
dfb348
dfb348
	mark    = armap->ar_first_ref_offset;
dfb348
	symrefs = m->armaps.armap_symrefs_32;
dfb348
dfb348
	for (idx=0,uch=*mark; idx
dfb348
		uref = (uch[0] << 24) + (uch[1] << 16) + (uch[2] << 8) + uch[3];
dfb348
		symrefs[idx].ar_member_offset = uref;
dfb348
	}
dfb348
dfb348
	armap->ar_string_table = m->symstrv;
dfb348
dfb348
	armapref = &m->armaps.armap_common_32;
dfb348
	armapref->ar_member         = memberp;
dfb348
	armapref->ar_symrefs        = symrefs;
dfb348
	armapref->ar_armap_sysv     = armap;
dfb348
	armapref->ar_armap_attr     = AR_ARMAP_ATTR_SYSV | AR_ARMAP_ATTR_BE_32;
dfb348
	armapref->ar_num_of_symbols = nsyms;
dfb348
	armapref->ar_size_of_refs   = nsyms * sizeof(*mark);
dfb348
	armapref->ar_size_of_strs   = cap - m->symstrs;
dfb348
	armapref->ar_string_table   = m->symstrs;
dfb348
dfb348
	m->armaps.armap_nsyms = nsyms;
dfb348
dfb348
	m->armeta.a_armap_primary.ar_armap_common_32 = armapref;
dfb348
dfb348
	return 0;
dfb348
}