[PATCH v4 2/2] ld: Add minimal pdb generation

Alan Modra amodra@gmail.com
Wed Oct 12 00:28:41 GMT 2022


On Tue, Oct 11, 2022 at 06:53:32PM +0100, Mark Harmstone wrote:
> +/* Calculate the hash of a given string.  */
> +static uint32_t
> +calc_hash (const char *data, size_t len)
> +{
> +  uint32_t hash = 0;
> +
> +  while (len >= 4)
> +    {
> +      hash ^= *(uint32_t *) data;
> +      data += 4;
> +      len -= 4;
> +    }
> +
> +  if (len >= 2)
> +    {
> +      hash ^= *(uint16_t *) data;
> +      data += 2;
> +      len -= 2;
> +    }
> +
> +  if (len != 0)
> +    hash ^= *data;
> +
> +  hash |= 0x20202020;
> +  hash ^= (hash >> 11);
> +
> +  return hash ^ (hash >> 16);
> +}

I think the above code will calculate different hash values on
big-endian machines to little-endian.  Also, unless "data" is aligned
as for uint32_t you run the risk of alignment traps on machines with
strict alignment requirements.

-- 
Alan Modra
Australia Development Lab, IBM


More information about the Binutils mailing list