0
struct libimg 
{
   Elf32_Phdr ph;
};

struct libimg limg = {
   {
      p_type: PT_LOAD,
      p_memsz: 2 * PAGE_SIZE
   }
};

static void makelib(void *r)
{
    limg.ph.p_vaddr = r;
}

And Elf32_Phdr is defined in linux/elf.h

typedef struct elf32_phdr{
   Elf32_Addr    p_vaddr;
   ....
 } Elf32_Phdr;

I want to assign p_vaddr value from the argument. But I get this warning assignment makes integer from pointer without a cast. I use gdb to check and print r shows (void *)0x08040000

11
  • And your question is.... ? Commented Oct 9, 2015 at 6:18
  • @Amit How do I fix this warning? Thanks for your time. Commented Oct 9, 2015 at 6:22
  • @HuangJie Which line exactly is producing the error? Commented Oct 9, 2015 at 6:25
  • post Elf32_Phdr type. Commented Oct 9, 2015 at 6:25
  • 1
    @Amit I don't think the compiler would generate a warning for converting from void* to another pointer type without a cast (unless p_vaddr isn't a pointer). Commented Oct 9, 2015 at 6:28

2 Answers 2

1

The compiler is warning you that an assignment caused an implicit cast. It does so because that might have been unintentional, and could potentially have undesirable effects. You could rectify that by using the correct type in the first place:

static void makelib(Elf32_Addr r)
{
    limg.ph.p_vaddr = r;
}

That might cause other code lines to generate warnings, and you should use a cast in these places, for example:

makelib((Elf32_Addr)someVariable);

The idea is to cast at the place where you change the meaning of a value from type A to type B, where it's a concise, deliberate decision. You should only do that (casting) if you can't use the proper type in the first place, that is, if someVariable can't be Elf32_Addr.

Sign up to request clarification or add additional context in comments.

3 Comments

usually we define p_vaddr in this way: p_vaddr: 0x40017000 just like p_type and p_memsz in the struct, but now its value is set to be 1073836032. Can we say they are the same?
@HuangJie Open your calculator and put hexadecimal value and convert it to decimal. You'll find that are the same value.
@LPs Thanks for your reply. Yes, I see it.
0

In Linux/arch/powerpc/boot/elf.h

 94 typedef struct elf32_phdr {
 95         Elf32_Word p_type;
 96         Elf32_Off p_offset;
 97         Elf32_Addr p_vaddr;
 98         Elf32_Addr p_paddr;
 99         Elf32_Word p_filesz;
100         Elf32_Word p_memsz;
101         Elf32_Word p_flags;
102         Elf32_Word p_align;
103 } Elf32_Phdr;

Where Elf32_Addr

typedef unsigned int Elf32_Addr;

Finally you are assigning a void * to a unsigned int

Cast it as unsigned int or better to Elf32_Addr to avoid warning:

limg.ph.p_vaddr = (Elf32_Addr)r;

3 Comments

No, either cast it to Elf32_Addr, or accept that as an input in the first place.
@Amit My solution works, probably not the coolest or the more standard one.
@LPs - I didn't say it doesn't work. HuangJie asked a question, I thought my answer relevant and appropriate. (It's not about being cool :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.