/*
copyright (c) 2012 Kazuki Iwamoto http://www.maid.org/ iwm@maid.org

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <windows.h>


int
main (int   argc,
      char *argv[])
{
  DWORD dwSize, dwWrite;
  HANDLE hOutput;
  IMAGE_FILE_HEADER ifh;
  IMAGE_SECTION_HEADER ish;
  IMAGE_SYMBOL is;
  LPCSTR lpszName = "__tls_array";

  hOutput = GetStdHandle (STD_OUTPUT_HANDLE);

  ifh.Machine = IMAGE_FILE_MACHINE_I386;
  ifh.NumberOfSections = 1;
  ifh.TimeDateStamp = 0;
  ifh.PointerToSymbolTable = sizeof (IMAGE_FILE_HEADER)
                           + sizeof (IMAGE_SECTION_HEADER);
  ifh.NumberOfSymbols = 3;
  ifh.SizeOfOptionalHeader = 0;
  ifh.Characteristics = 0;
  WriteFile (hOutput, &ifh, sizeof (IMAGE_FILE_HEADER), &dwWrite, NULL);

  ish.Name[0] = '.';
  ish.Name[1] = 'd';
  ish.Name[2] = 'a';
  ish.Name[3] = 't';
  ish.Name[4] = 'a';
  ish.Name[5] = '\0';
  ish.Name[6] = '\0';
  ish.Name[7] = '\0';
  ish.Misc.VirtualSize = 0;
  ish.VirtualAddress = 0;
  ish.SizeOfRawData = 0;
  ish.PointerToRawData = 0;
  ish.PointerToRelocations = 0;
  ish.PointerToLinenumbers = 0;
  ish.NumberOfRelocations = 0;
  ish.NumberOfLinenumbers = 0;
  ish.Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA
                      | IMAGE_SCN_ALIGN_1BYTES
                      | IMAGE_SCN_ALIGN_8BYTES
                      | IMAGE_SCN_MEM_READ
                      | IMAGE_SCN_MEM_WRITE;
  WriteFile (hOutput, &ish, sizeof (IMAGE_SECTION_HEADER), &dwWrite, NULL);

  is.N.ShortName[0] = '.';
  is.N.ShortName[1] = 'd';
  is.N.ShortName[2] = 'a';
  is.N.ShortName[3] = 't';
  is.N.ShortName[4] = 'a';
  is.N.ShortName[5] = '\0';
  is.N.ShortName[6] = '\0';
  is.N.ShortName[7] = '\0';
  is.Value = 0;
  is.SectionNumber = 1;
  is.Type = IMAGE_SYM_TYPE_NULL | IMAGE_SYM_DTYPE_NULL << N_BTSHFT;
  is.StorageClass = IMAGE_SYM_CLASS_STATIC;
  is.NumberOfAuxSymbols = 1;
  WriteFile (hOutput, &is, sizeof (IMAGE_SYMBOL), &dwWrite, NULL);

  is.N.ShortName[0] = '\0';
  is.N.ShortName[1] = '\0';
  is.N.ShortName[2] = '\0';
  is.N.ShortName[3] = '\0';
  is.N.ShortName[4] = '\0';
  is.N.ShortName[5] = '\0';
  is.N.ShortName[6] = '\0';
  is.N.ShortName[7] = '\0';
  is.Value = 0;
  is.SectionNumber = 0;
  is.Type = IMAGE_SYM_TYPE_NULL | IMAGE_SYM_DTYPE_NULL << N_BTSHFT;
  is.StorageClass = IMAGE_SYM_CLASS_NULL;
  is.NumberOfAuxSymbols = 0;
  WriteFile (hOutput, &is, sizeof (IMAGE_SYMBOL), &dwWrite, NULL);

  is.N.Name.Short = 0;
  is.N.Name.Long = 4;
  is.Value = 0x2c;
  is.SectionNumber = IMAGE_SYM_ABSOLUTE;
  is.Type = IMAGE_SYM_TYPE_NULL | IMAGE_SYM_DTYPE_NULL << N_BTSHFT;
  is.StorageClass = IMAGE_SYM_CLASS_EXTERNAL;
  is.NumberOfAuxSymbols = 0;
  WriteFile (hOutput, &is, sizeof (IMAGE_SYMBOL), &dwWrite, NULL);

  dwSize = sizeof (DWORD) + (lstrlenA (lpszName) + 1) * sizeof (CHAR);
  WriteFile (hOutput, &dwSize, sizeof (DWORD), &dwWrite, NULL);

  dwSize -= sizeof (DWORD);
  WriteFile (hOutput, lpszName, dwSize, &dwWrite, NULL);

  return 0;
}
