+
+ if (!bfd_check_format (pbfd, bfd_object))
+ error ("It doesn't seem to be an object file");
+
+ for (section = pbfd->sections; section; section = section->next)
+ {
+ if (bfd_get_section_flags (pbfd, section) & SEC_ALLOC)
+ {
+ UDIResource To;
+ UDICount Count;
+ unsigned long section_size, section_end;
+ const char *section_name;
+
+ section_name = bfd_get_section_name (pbfd, section);
+ if (STREQ (section_name, ".text") && !load_text)
+ continue;
+ else if (STREQ (section_name, ".data") && !load_data)
+ continue;
+ else if (STREQ (section_name, ".bss") && !load_bss)
+ continue;
+ else if (STREQ (section_name, ".lit") && !load_lit)
+ continue;
+
+ To.Offset = bfd_get_section_vma (pbfd, section);
+ section_size = bfd_section_size (pbfd, section);
+ section_end = To.Offset + section_size;
+
+ printf("[Loading section %s at %x (%d bytes)]\n",
+ section_name,
+ To.Offset,
+ section_size);
+
+ if (bfd_get_section_flags (pbfd, section) & SEC_CODE)
+ {
+ To.Space = UDI29KIRAMSpace;
+
+ address_ranges[0].Offset = min (address_ranges[0].Offset,
+ To.Offset);
+ address_ranges[0].Size = max (address_ranges[0].Size,
+ section_end
+ - address_ranges[0].Offset);
+ }
+ else
+ {
+ To.Space = UDI29KDRAMSpace;
+
+ address_ranges[1].Offset = min (address_ranges[1].Offset,
+ To.Offset);
+ address_ranges[1].Size = max (address_ranges[1].Size,
+ section_end
+ - address_ranges[1].Offset);
+ }
+
+ if (bfd_get_section_flags (pbfd, section) & SEC_LOAD) /* Text, data or lit */
+ {
+ file_ptr fptr;
+
+ fptr = 0;
+
+ while (section_size > 0)
+ {
+ char buffer[1024];
+
+ Count = min (section_size, 1024);
+
+ bfd_get_section_contents (pbfd, section, buffer, fptr,
+ Count);
+
+ err = UDIWrite ((UDIHostMemPtr)buffer, /* From */
+ To, /* To */
+ Count, /* Count */
+ (UDISizeT)1, /* Size */
+ &Count, /* CountDone */
+ (UDIBool)0); /* HostEndian */
+ if (err)
+ error ("UDIWrite failed, error = %d", err);
+
+ To.Offset += Count;
+ fptr += Count;
+ section_size -= Count;
+ }
+ }
+ else /* BSS */
+ {
+ UDIResource From;
+ unsigned long zero = 0;
+
+ /* Write a zero byte at the vma */
+ err = UDIWrite ((UDIHostMemPtr)&zero, /* From */
+ To, /* To */
+ (UDICount)1, /* Count */
+ (UDISizeT)4, /* Size */
+ &Count, /* CountDone */
+ (UDIBool)0); /* HostEndian */
+ if (err)
+ error ("UDIWrite failed, error = %d", err);
+
+ From = To;
+ To.Offset+=4;
+
+ /* Now, duplicate it for the length of the BSS */
+ err = UDICopy (From, /* From */
+ To, /* To */
+ (UDICount)(section_size/4 - 1), /* Count */
+ (UDISizeT)4, /* Size */
+ &Count, /* CountDone */
+ (UDIBool)1); /* Direction */
+ if (err)
+ {
+ char message[100];
+ int xerr;
+
+ xerr = UDIGetErrorMsg(err, 100, message, &Count);
+ if (!xerr)
+ fprintf (stderr, "Error is %s\n", message);
+ else
+ fprintf (stderr, "xerr is %d\n", xerr);
+ error ("UDICopy failed, error = %d", err);
+ }
+ }
+
+ }
+ }
+
+ entry.Space = UDI29KIRAMSpace;
+ entry.Offset = bfd_get_start_address (pbfd);
+