#if OS_WINDOWS

#define SY__PE_MSDOS_MAGIC 0x5A4D
struct SY__PE_DosHeader{
  U16 magic;
  U16 last_page_size;
  U16 page_count;
  U16 reloc_count;
  U16 paragraph_header_size;
  U16 min_paragraph;
  U16 max_paragraph;
  U16 init_ss;
  U16 init_sp;
  U16 checksum;
  U16 init_ip;
  U16 init_cs;
  U16 reloc_table_file_off;
  U16 overlay_number;
  U16 reserved[4];
  U16 oem_id;
  U16 oem_info;
  U16 reserved2[10];
  U32 coff_file_offset;
};
#define SY__PE_SIGNATURE 0x00004550
struct SY__PE_CoffHeader{
  U16 machine_type;
  U16 section_count;
  U32 time_stamp;
  U32 symbol_table_foff;
  U32 symbol_count;
  U16 optional_header_size;
  U16 flags;
};
struct SY__PE_SectionHeader{
  U8  name[8];
  U32 vsize;
  U32 voff;
  U32 fsize;
  U32 foff;
  U32 relocations_foff;
  U32 line_numbers_foff;
  U16 relocation_count;
  U16 line_number_count;
  U32 flags;
};

void
sy__section_init(char *name, U32 name_size, void **first_out, void **opl_out){
  if (name_size <= 8){
    extern U8 __ImageBase[];
    U8 *base = __ImageBase;
    struct SY__PE_DosHeader *dos = (struct SY__PE_DosHeader*)base;
    if (dos->magic == SY__PE_MSDOS_MAGIC){
      U32 *pe_signature = (U32*)(base + dos->coff_file_offset);
      if (*pe_signature == SY__PE_SIGNATURE){
        struct SY__PE_CoffHeader *coff = (struct SY__PE_CoffHeader*)(pe_signature + 1);
        U32 sections_offset = (dos->coff_file_offset + sizeof(*pe_signature) +
                               sizeof(*coff) + coff->optional_header_size);
        U32 section_count = coff->section_count;
        struct SY__PE_SectionHeader*
          section = (struct SY__PE_SectionHeader*)(base + sections_offset);
        for (U32 i = 0; i < section_count; i += 1, section += 1){
          int match = 1;
          for (U32 j = 0; j < 8; j += 1){
            if (name[j] != section->name[j]){
              match = 0;
              break;
            }
            if (name[j] == 0){
              break;
            }
          }
          if (match){
            *first_out = base + section->voff;
            *opl_out = base + section->voff + section->vsize;
            break;
          }
        }
      }
    }
  }
}

#endif
