www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2017/04/28/11:24:56

X-Authentication-Warning: delorie.com: mail set sender to djgpp-bounces using -f
X-Recipient: djgpp AT delorie DOT com
X-Original-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20161025;
h=mime-version:in-reply-to:references:from:date:message-id:subject:to;
bh=Tr+55KCEAx0ADoruPlMwedUdYwdtp5/xOvQTOqs6pBE=;
b=WP7TZy0G7yY9MnKlQ3EibYqWsPnkl5LjXOaJrY8Y5jTgJRl+cwLRWDaPDFMQySrt9G
5B65WmgcdXiQpUaf94vI0hRagiG9Aeex03+i/jy7E+fkm3zp80OWhmMcVDMwbgEdQ4ot
a4V52bxypHMsM+8r67ACSBCBeW7iDAA/aVy3ZKYigZov0l+c8xeJg+Hw8UGteBTvbbeo
HbjH72F9FH7fwVU1Gwo+yfTVYKew9Bsy2GLNQTHPcf14WyBR3kzghiiov3BxBzi8t8zO
sGsPsVDlorz+T3WS6laKF6mUKoRhwGp1+xsRcZ7v/TAGGQyulxWxCcvjH/o6ajPNfP6T
nPbg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20161025;
h=x-gm-message-state:mime-version:in-reply-to:references:from:date
:message-id:subject:to;
bh=Tr+55KCEAx0ADoruPlMwedUdYwdtp5/xOvQTOqs6pBE=;
b=sLZCUFLsxXiyeSvhuVC5tXjCQDdrEK2N0rY2wiER4a3eIUJPR0Gn5fvpW1LkkMNXxZ
IhGiRw0/epPdFYtB/Ye8iwMHl4X1+9PYJ1Gx2Jn4L2dlDqep2M/XGzaOu3e4JiI2kIbh
5HIjTkNpSxHHz7cncHGTh7FI4mlxZ1GbkMkEJEmwOW9inLU5skQH89MuJRQdm8ktGqBV
aIqWpwspdrzj0YXh/8KWNnK0xBKX3jQGX/sYT+24szQfZq5r90gn+C9bfpQop+J0WBJs
C6ly4RcaVpw59V4Fd/gxfGK+nlypS4WRiWgqpkIqhRw2l9oYWgGW/RYMC2m7IoDZbeYM
hNdQ==
X-Gm-Message-State: AN3rC/60RVHKuNYj66hka7IqBjSrpkKTz9PZomxGC71YEVxkEyhpOUnn
C+dZagz24iTh/q+od8hY83TQDctMNLED
X-Received: by 10.237.36.5 with SMTP id r5mr9870680qtc.253.1493393084223; Fri,
28 Apr 2017 08:24:44 -0700 (PDT)
MIME-Version: 1.0
In-Reply-To: <CAA2C=vBNvQsBU+2_0JhJQqJr41pV=VWTUxgyS29VjUK6zL3paA@mail.gmail.com>
References: <CAA2C=vC8s9Kg9LhewQmUMtiJNm+f=Gu5b-m_QXgiTgNufQQpfQ AT mail DOT gmail DOT com>
<CAA2C=vBNvQsBU+2_0JhJQqJr41pV=VWTUxgyS29VjUK6zL3paA AT mail DOT gmail DOT com>
From: "Ozkan Sezer (sezeroz AT gmail DOT com) [via djgpp AT delorie DOT com]" <djgpp AT delorie DOT com>
Date: Fri, 28 Apr 2017 18:24:43 +0300
Message-ID: <CAA2C=vDKwsv7i5jNOGEpOEdokwsWbP3MV_eHU_M6s79cn=NkZg@mail.gmail.com>
Subject: Re: dxe3gen does not handle relocs > 65535
To: djgpp AT delorie DOT com
Reply-To: djgpp AT delorie DOT com
Errors-To: nobody AT delorie DOT com
X-Mailing-List: djgpp AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

On 4/28/17, Ozkan Sezer <sezeroz AT gmail DOT com> wrote:
> On 4/28/17, Ozkan Sezer <sezeroz AT gmail DOT com> wrote:
>> dxe3gen does not handle extended relocations, i.e. STYP_NRELOC_OVFL
>> set in hdr->s_flags: it still takes hdr->s_nreloc as number of relocs
>> and ends up generating a broken dxe which leads to bad crashes when
>> loaded.  We have one such c++ project where s_nreloc does overflow.
>>
>> I will be trying a few things with this, but I'd appreciate help or
>> advice or links too:  How should I read the actual number of relocs?
>> Would our current dlopen() still work with this?
>
> Here is a dirty draft (to be tested yet). Comments?
>
> Index: dxe3gen.c
> ===================================================================
> RCS file: /cvs/djgpp/djgpp/src/dxe/dxe3gen.c,v
> retrieving revision 1.23
> diff -u -p -r1.23 dxe3gen.c
> --- dxe3gen.c	4 Oct 2015 10:27:26 -0000	1.23
> +++ dxe3gen.c	28 Apr 2017 08:37:50 -0000
> @@ -937,6 +937,7 @@ static int write_dxe(FILE *inf, FILE *ou
>    char *strings;
>    RELOC *relocs;
>    unsigned int i, j, errcount;
> +  ULONG32 real_nrelocs;
>    size_t hdrsize;
>
>    /* Exported symbols table */
> @@ -953,7 +954,18 @@ static int write_dxe(FILE *inf, FILE *ou
>
>    dh.magic = DXE_MAGIC;
>    dh.element_size = -1;
> -  dh.nrelocs = sc.s_nreloc;
> +  if (sc.s_flags & STYP_NRELOC_OVFL) {
> +    long pos = ftell(inf);
> +    fseek(inf, sc.s_relptr, SEEK_SET);
> +    /* r_vaddr is 1st member of struct external_reloc */
> +    fread(&real_nrelocs, 4, 1, inf);
> +    fseek(inf, pos, SEEK_SET);
> +    dh.nrelocs = --real_nrelocs; /* remove the '+1' */
> +    fprintf(stderr, "%s: real nrelocs: %u\n", progname, real_nrelocs);
> +  }
> +  else {
> +    dh.nrelocs = real_nrelocs = sc.s_nreloc;
> +  }
>    dh.n_exp_syms = 0;
>    dh.exp_table = sizeof(dh);
>    dh.n_deps = opt.num_deps;
> @@ -985,9 +997,12 @@ static int write_dxe(FILE *inf, FILE *ou
>    strings[0] = 0;
>
>    /* Read the relocation table */
> -  relocs = (RELOC *)malloc(sc.s_nreloc * sizeof(RELOC));
> +  relocs = (RELOC *)malloc(real_nrelocs * sizeof(RELOC));
>    fseek(inf, sc.s_relptr, SEEK_SET);
> -  fread(relocs, RELSZ, sc.s_nreloc, inf);
> +  if (sc.s_flags & STYP_NRELOC_OVFL) {
> +    fseek(inf, RELSZ, SEEK_CUR); /* skip the extra one. */
> +  }
> +  fread(relocs, RELSZ, real_nrelocs, inf);
>
>    /* Close input file */
>    fclose(inf);
> @@ -1042,7 +1057,7 @@ static int write_dxe(FILE *inf, FILE *ou
>        int n_abs_relocs = 0, n_rel_relocs = 0;
>
>        /* count the amount of relocations pointing to this symbol */
> -      for (j = 0; j < sc.s_nreloc; j++)
> +      for (j = 0; j < real_nrelocs; j++)
>        {
>          if (relocs[j].r_symndx == i)
>          {
> @@ -1101,7 +1116,7 @@ static int write_dxe(FILE *inf, FILE *ou
>
>        unres_size = newsize;
>
> -      for (j = 0; j < sc.s_nreloc; j++)
> +      for (j = 0; j < real_nrelocs; j++)
>        {
>          if (relocs[j].r_symndx == i)
>          {
> @@ -1202,7 +1217,7 @@ static int write_dxe(FILE *inf, FILE *ou
>
>    /* Compute the amount of valid relocations */
>    DEBUG_PRINT_RELOCATION_DIRECTIVE_PROLOG();
> -  for (i = 0; i < sc.s_nreloc; i++)
> +  for (i = 0; i < real_nrelocs; i++)
>    {
>      DEBUG_PRINT_RELOCATION_DIRECTIVE(i, relocs);
>      if (!VALID_RELOC(relocs[i]))
> @@ -1278,7 +1293,7 @@ static int write_dxe(FILE *inf, FILE *ou
>    free(data);
>
>    /* Output the relocations */
> -  for (i = 0; i < sc.s_nreloc; i++)
> +  for (i = 0; i < real_nrelocs; i++)
>    {
>      if (VALID_RELOC(relocs[i]))
>        fwrite(&relocs[i].r_vaddr, 1, sizeof(relocs[0].r_vaddr), outf);
>

Just tested the above patch by generating out dxe and loading
and running with it: works fine for me.

Any gotchas anyone can notice? Any comments and/or suggestions?
Should apply to CVS?

--
O.S.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019