host_pointer_to_address() === (CORE_ADDR) (void*) (val)

Andrew Cagney ac131313@cygnus.com
Fri May 26 04:03:00 GMT 2000


Hello,

If you look through code like procfs.c, you will find casts like:

      /* Stop looping if the callback returns non-zero.  */
      if ((funcstat = (*func) (fd, (CORE_ADDR) map->pr_vaddr)) != 0)

The problem is with ``(CORE_ADDR) map->pr_vaddr''.  ``map->pr_vaddr'' is
a ``void *'' (32bits) and ``CORE_ADDR'' is a ``long long'' (64bits). 
Some (one? mips/n32) targets assume that addresses sign-extended while
others assume zero extension.  The consequence is that the above code is
potentially dangerous.  GCC complains with a warning while some native
compilers refuse to accept it at all :-/

To address this, I'd like to propose two wrappers to JimB's
POINTER_TO_ADDRESS and ADDRESS_TO_POINTER:

	CORE_ADDR host_pointer_to_address (void *ptr);
		... check operation is sane ...
		return POINTER_TO_ADDRESS (builtin_type_ptr, ptr);
	void *address_to_host_pointer (CORE_ADDR addr);
		void *ptr;
		... check operation is sane ...
		ADDRESS_TO_POINTER (builtin_type_ptr, &ptr, addr)
		return ptr;

which would be used when ever a host void* <-> CORE_ADDR conversion was
required.  The above code would then be rewritten as:

	funcstat = func (fd, host_pointer_to_address (map->pr_vaddr));
	if (funcstat != 0)
	  ....

this would ensure that the code passed around a correct ``CORE_ADDR''
(and not a truncated value that just happens to work).  This becomes
very important when you start debugging n32 abi's (32 bit pointers)
where the target has 64 bit registers - the full sign extended value
needs to be read/written.

Thoughts?  Making sense?  I'll post a full patch for irix[56]/n32 once
I've seen some test results.

	Andrew


More information about the Gdb-patches mailing list