Blame src/arbits/slbt_armap_sysv_64.c

b5595d
/*******************************************************************/
b5595d
/*  slibtool: a skinny libtool implementation, written in C        */
b5595d
/*  Copyright (C) 2016--2024  SysDeer Technologies, LLC            */
b5595d
/*  Released under the Standard MIT License; see COPYING.SLIBTOOL. */
b5595d
/*******************************************************************/
b5595d
b5595d
#include <stdint.h>
b5595d
#include <stddef.h>
b5595d
#include <stdlib.h>
b5595d
#include <string.h>
b5595d
b5595d
#include <slibtool/slibtool.h>
b5595d
#include <slibtool/slibtool_arbits.h>
b5595d
#include "slibtool_ar_impl.h"
b5595d
#include "slibtool_driver_impl.h"
b5595d
#include "slibtool_errinfo_impl.h"
4b56de
#include "slibtool_visibility_impl.h"
b5595d
4b56de
slbt_hidden int slbt_ar_parse_primary_armap_sysv_64(
b5595d
	const struct slbt_driver_ctx *  dctx,
b5595d
	struct slbt_archive_meta_impl * m)
b5595d
{
b5595d
	struct ar_raw_armap_sysv_64 *   armap;
b5595d
	struct ar_meta_member_info *    memberp;
b5595d
	struct ar_meta_armap_common_64 *armapref;
b5595d
	struct ar_meta_armap_ref_64 *   symrefs;
b5595d
	uint64_t                        idx;
b5595d
	uint64_t                        uref_hi;
b5595d
	uint64_t                        uref_lo;
b5595d
	uint64_t                        nsyms_hi;
b5595d
	uint64_t                        nsyms_lo;
b5595d
	uint64_t                        nsyms;
b5595d
	uint64_t                        nstrs;
b5595d
	const char *                    ch;
b5595d
	const char *                    cap;
b5595d
	unsigned char *                 uch;
b5595d
	unsigned char                   (*mark)[0x08];
b5595d
b5595d
	armap   = &m->armaps.armap_sysv_64;
b5595d
	memberp = m->memberv[0];
b5595d
b5595d
	mark = memberp->ar_object_data;
b5595d
b5595d
	armap->ar_num_of_syms = mark;
b5595d
	uch = *mark++;
b5595d
b5595d
	armap->ar_first_ref_offset = mark;
b5595d
b5595d
	nsyms_hi = (uch[0] << 24) + (uch[1] << 16) + (uch[2] << 8) + uch[3];
b5595d
	nsyms_lo = (uch[4] << 24) + (uch[5] << 16) + (uch[6] << 8) + uch[7];
b5595d
b5595d
	nsyms = (nsyms_hi << 32) + nsyms_lo;
b5595d
	mark += nsyms;
b5595d
b5595d
	if (memberp->ar_object_size < (sizeof(*mark) + (nsyms * sizeof(*mark))))
b5595d
		return SLBT_CUSTOM_ERROR(
b5595d
			dctx,
b5595d
			SLBT_ERR_AR_INVALID_ARMAP_NUMBER_OF_SYMS);
b5595d
b5595d
	m->symstrs = (const char *)mark;
b5595d
b5595d
	cap  = memberp->ar_object_data;
b5595d
	cap += memberp->ar_object_size;
b5595d
b5595d
	if ((cap == m->symstrs) && nsyms)
b5595d
		return SLBT_CUSTOM_ERROR(
b5595d
			dctx,
b5595d
			SLBT_ERR_AR_INVALID_ARMAP_STRING_TABLE);
b5595d
b5595d
	if (nsyms && !m->symstrs[0])
b5595d
		return SLBT_CUSTOM_ERROR(
b5595d
			dctx,
b5595d
			SLBT_ERR_AR_INVALID_ARMAP_STRING_TABLE);
b5595d
b5595d
	for (ch=&m->symstrs[1],nstrs=0; ch
b5595d
		if (!ch[0] && !ch[-1] && (nstrs < nsyms))
b5595d
			return SLBT_CUSTOM_ERROR(
b5595d
				dctx,
b5595d
				SLBT_ERR_AR_INVALID_ARMAP_STRING_TABLE);
b5595d
b5595d
		if (!ch[0] && ch[-1])
b5595d
			nstrs++;
b5595d
	}
b5595d
b5595d
	if (nstrs != nsyms)
b5595d
		return SLBT_CUSTOM_ERROR(
b5595d
			dctx,
b5595d
			SLBT_ERR_AR_INVALID_ARMAP_STRING_TABLE);
b5595d
b5595d
	if (cap[-1])
b5595d
		return SLBT_CUSTOM_ERROR(
b5595d
			dctx,
b5595d
			SLBT_ERR_AR_INVALID_ARMAP_STRING_TABLE);
b5595d
b5595d
	if (!(m->symstrv = calloc(nsyms + 1,sizeof(const char *))))
b5595d
		return SLBT_SYSTEM_ERROR(dctx,0);
b5595d
b5595d
	if (!(m->armaps.armap_symrefs_64 = calloc(nsyms + 1,sizeof(*symrefs))))
b5595d
		return SLBT_SYSTEM_ERROR(dctx,0);
b5595d
b5595d
	mark    = armap->ar_first_ref_offset;
b5595d
	symrefs = m->armaps.armap_symrefs_64;
b5595d
b5595d
	for (idx=0,uch=*mark; idx
b5595d
		uref_hi = (uch[0] << 24) + (uch[1] << 16) + (uch[2] << 8) + uch[3];
b5595d
		uref_lo = (uch[4] << 24) + (uch[5] << 16) + (uch[6] << 8) + uch[7];
b5595d
b5595d
		symrefs[idx].ar_member_offset = (uref_hi << 32) + uref_lo;
b5595d
	}
b5595d
b5595d
	armap->ar_string_table = m->symstrv;
b5595d
b5595d
	armapref = &m->armaps.armap_common_64;
b5595d
	armapref->ar_member         = memberp;
b5595d
	armapref->ar_symrefs        = symrefs;
b5595d
	armapref->ar_armap_sysv     = armap;
b5595d
	armapref->ar_armap_attr     = AR_ARMAP_ATTR_SYSV | AR_ARMAP_ATTR_BE_64;
b5595d
	armapref->ar_num_of_symbols = nsyms;
b5595d
	armapref->ar_size_of_refs   = nsyms * sizeof(*mark);
b5595d
	armapref->ar_size_of_strs   = cap - m->symstrs;
b5595d
	armapref->ar_string_table   = m->symstrs;
b5595d
b5595d
	m->armaps.armap_nsyms = nsyms;
b5595d
b5595d
	m->armeta.a_armap_primary.ar_armap_common_64 = armapref;
b5595d
b5595d
	return 0;
b5595d
}