Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
3.5 KiB

  1. #include <assert.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <windows.h>
  5. #include "prodids.h"
  6. #include "version.h"
  7. void
  8. StoreName(PUCHAR sz, PIMAGE_SYMBOL psym, PULONG pcbStrings)
  9. {
  10. memset((void *)&psym->N.Name, 0, (size_t)sizeof(psym->N.Name));
  11. if (strlen(sz) <= IMAGE_SIZEOF_SHORT_NAME) {
  12. strncpy((char *)psym->N.ShortName, (char *)sz, IMAGE_SIZEOF_SHORT_NAME);
  13. } else {
  14. psym->N.Name.Long = *pcbStrings;
  15. *pcbStrings += strlen(sz) + 1;
  16. }
  17. }
  18. int main(int argc, char **argv)
  19. {
  20. PUCHAR szFrom, szTo, szFilename;
  21. FILE *pfile;
  22. IMAGE_FILE_HEADER hdr;
  23. IMAGE_SYMBOL sym;
  24. IMAGE_AUX_SYMBOL aux;
  25. ULONG cbStrings, foStringTable;
  26. IMAGE_SECTION_HEADER sec;
  27. DWORD dwProdid;
  28. if (argc != 4) {
  29. printf("usage: ALIASOBJ from-name to-name output-filename\n");
  30. exit(1);
  31. }
  32. szFrom = argv[1];
  33. szTo = argv[2];
  34. szFilename = argv[3];
  35. if ((pfile = fopen(szFilename, "wb")) == NULL) {
  36. printf("can't open file \"%s\"\n", szFilename);
  37. exit(1);
  38. }
  39. hdr.Machine = IMAGE_FILE_MACHINE_UNKNOWN;
  40. hdr.NumberOfSections = 1;
  41. hdr.TimeDateStamp = (DWORD) time(NULL);
  42. hdr.PointerToSymbolTable = sizeof(hdr) + sizeof(sec);
  43. hdr.NumberOfSymbols = 4;
  44. hdr.SizeOfOptionalHeader = 0;
  45. hdr.Characteristics = 0;
  46. fwrite(&hdr, sizeof(hdr), 1, pfile);
  47. // Generate one section header. This is necessary because of a link bug (vce:182)
  48. // which fails for .obj's with 0 sections and unknown machine type.
  49. //
  50. memset(&sec, 0, sizeof(sec));
  51. strcpy(sec.Name, ".text");
  52. sec.Characteristics = IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_EXECUTE;
  53. fwrite(&sec, sizeof(sec), 1, pfile);
  54. cbStrings = sizeof(ULONG); // make room for stringtab size
  55. #if (rmj == 7) && (rmm == 0)
  56. dwProdid = DwProdidFromProdidWBuild(prodidAliasObj70, rup);
  57. #else
  58. #error PRODID needs to be assigned
  59. #endif
  60. memset(sym.N.ShortName, 0, sizeof(sym.N.ShortName));
  61. strncpy((char *) sym.N.ShortName, symProdIdentName, IMAGE_SIZEOF_SHORT_NAME);
  62. sym.Value = dwProdid;
  63. sym.SectionNumber = symProdIdentSection;
  64. sym.Type = IMAGE_SYM_TYPE_NULL;
  65. sym.StorageClass = symProdIdentClass;
  66. sym.NumberOfAuxSymbols = 0;
  67. fwrite(&sym, sizeof(sym), 1, pfile);
  68. StoreName(szTo, &sym, &cbStrings);
  69. sym.Value = 0;
  70. sym.SectionNumber = IMAGE_SYM_UNDEFINED;
  71. sym.Type = IMAGE_SYM_TYPE_NULL;
  72. sym.StorageClass = IMAGE_SYM_CLASS_EXTERNAL;
  73. sym.NumberOfAuxSymbols = 0;
  74. fwrite(&sym, sizeof(sym), 1, pfile);
  75. StoreName(szFrom, &sym, &cbStrings);
  76. sym.Value = 0;
  77. sym.SectionNumber = IMAGE_SYM_UNDEFINED;
  78. sym.Type = IMAGE_SYM_TYPE_NULL;
  79. sym.StorageClass = IMAGE_SYM_CLASS_WEAK_EXTERNAL;
  80. sym.NumberOfAuxSymbols = 1;
  81. fwrite(&sym, sizeof(sym), 1, pfile);
  82. memset(&aux, 0, sizeof(aux));
  83. aux.Sym.Misc.TotalSize = IMAGE_WEAK_EXTERN_SEARCH_ALIAS;
  84. aux.Sym.TagIndex = 1; // symtab index for extern
  85. fwrite(&aux, sizeof(aux), 1, pfile);
  86. // Write the string table.
  87. //
  88. foStringTable = ftell(pfile);
  89. fwrite(&cbStrings, sizeof(cbStrings), 1, pfile);
  90. if (strlen(szTo) > IMAGE_SIZEOF_SHORT_NAME) {
  91. fputs(szTo, pfile);
  92. fputc(0, pfile);
  93. }
  94. if (strlen(szFrom) > IMAGE_SIZEOF_SHORT_NAME) {
  95. fputs(szFrom, pfile);
  96. fputc(0, pfile);
  97. }
  98. assert(ftell(pfile) - foStringTable == cbStrings);
  99. fclose(pfile);
  100. return 0;
  101. }